Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Originally I was trying to send a Javascript JSON Object to a PHP page, without success. Using the following test code:

var testobj = {
  "test1":"test1data",
  "test2":"test2data",
  "test3":"test3data"
};
alert(testobj.test1);
$.ajax({
  type: "POST",
  url: "ajaxtest.php",
  dataType: "json",
  data: testobj
})
  .error(function( msg ) {
    alert( "Error: " + msg );
  })
  .success(function( msg ) {
    alert( "Success: " + msg );
  });

Never sends the data, and always returns an error.

Removing the 'dataType' attribute or defining it as anything other than 'json' returns 'success', but still no data is sent.

I can use the exact same Object code as the 'data:' value and it DOES get sent, but not when I use the JSON Object.

So I send an Array, instead:

var testobj = new Array();
testobj = {
  "test1":"test1data",
  "test2":"test2data",
  "test3":"test3data"
};
alert(testobj.test1);
$.ajax({
  type: "POST",
  url: "ajaxtest.php",
  data: testobj
})
  .error(function( msg ) {
    alert( "Error: " + msg );
  })
  .success(function( msg ) {
    alert( "Success: " + msg );
  });

Exactly the same code, except (a) I defined the variable as an Array instead of a JSON Object and (b) I removed the 'dataType' attribute from the .ajax call.

Grab the Array elements on the target page using the $_POST array.

I never much cared for JSON as a method for passing data, anyway, so this doesn't exactly worry me. But I do wonder about the current state of jQuery (v.1.9.1)

Is there something wrong with my JSON construction? What's the deal with sending a JSON Object via jQuery AJAX? And why is there a problem with the 'dataType: "json"' attribute?

share|improve this question
1  
A few notes. testobj = {...} replaces whatever was in testobj with an object. dataType: "json" is the data type of the data returned from the server. You get an error because the server is not returning JSON, and jQuery is trying to parse it. –  Rocket Hazmat Nov 7 '13 at 22:40
    
@RocketHazmat you should post as answer. spot on –  A.O. Nov 7 '13 at 22:43
    
Well, there you go! I think I'll take a break ... :) –  James Butler Nov 7 '13 at 23:49

1 Answer 1

up vote 4 down vote accepted

The first thing here is to realize what the dataType parameter is actually for. It's used to tell jQuery what you are expecting to receive from the server. You are getting an error because you told jQuery to expect JSON, but that's not what you are giving it. It's trying to parse it and fails. The data is being sent to the server.

Second, you are not sending JSON to your PHP script. In both examples (which are the same, btw), jQuery is converting the object (not JSON [see note below]) to a query string and sending that via POST, just like a normal <form> POST would.

Third, your "array" creation.

var testobj = new Array();
testobj = {
  "test1":"test1data",
  "test2":"test2data",
  "test3":"test3data"
};

This doesn't do what you think it does. This creates a new array then immediately throws it away and uses an object instead (you are setting testobj to an object, same as the first example). Note that arrays in JavaScript are numeric only, it does not have "associative arrays".

P.S. There is no such thing as a "JSON Object". You are not using JSON anywhere in this code. JSON is a string representation of data that has similar syntax to JavaScript arrays/objects. If it's not a string, it's not JSON.

P.P.S. Don't use .success() and .error(), they are deprecated. Use .done() and .fail(). See: http://api.jquery.com/jQuery.ajax/#jqXHR

share|improve this answer
    
You are absolutely correct. Thank you for learning me real good! :) –  James Butler Nov 7 '13 at 23:50
    
As I reflect on this ... I honestly don't see the difference between my "object" and a simple JSON "string". Could you enlighten me, please? Why is that not considered JSON syntax? (Info gleaned from json.org, if I understood it.) –  James Butler Nov 7 '13 at 23:58
    
@JamesButler: JSON is a when it's is a string. This is JSON: var x = '{"your": "data", "test": [1,2,3]}'; Notice how x is a string. It just so happens to contain something that can be parsed into an object. This might help: benalman.com/news/2010/03/theres-no-such-thing-as-a-json –  Rocket Hazmat Nov 8 '13 at 0:10
    
Thank you. I got a bit confused by json.org which includes "object" as a valid JSON construct, much as I used, above. I appreciate your help. –  James Butler Nov 8 '13 at 0:17
    
@JamesButler: Think of that as what it gets decoded as. {} gets decoded to an object. –  Rocket Hazmat Nov 8 '13 at 0:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.