JQuery doesn't handle json automatically

Here is my client side jQuery code:

$.ajaxSetup ({
   contentType: "application/json",
   datatype: 'json'
});

$.ajax({
   type: "POST",
   url: "http://localhost:1234/path",
   data: JSON.stringify(myData),
   success: function(aString){
      alert(aString);
   },
   error: function(errorData){
      alert(errorData);
   }
});

Here is the data that the server sends:

200
Content-Type: application/json

"aStringsData"

The warning displays quotation marks "aStringData". However, I expect the quotes to be selected due to the automatic JSON.parse that I expect from the data type: "json". Am I wrong about this?

+5
source share
3 answers

Actually a parameter dataType, not dataType(JavaScript is case sensitive).

You can try:

dataType: 'json' // not datatype

inside ajaxSetup;

+5
source

Your Answer:

"aStringsData" 

not valid. JSONI believe that JSON should start with {, right ?.

You can keep in mind

{
 "string": "data"
}
0
source

you can try using something like this:

eval(aString)
-1
source

All Articles