Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery doesn't seem to parse JSON automatically

Here's 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 the server sends out:

200
Content-Type: application/json

"aStringsData"

In the alert the quotes of "aStringData" are displayed. However, I'd expect the quotes to be taken away due to the automatic JSON.parse I expect to happen from datatype: 'json'. Am I wrong on this?

like image 557
hansi Avatar asked Oct 08 '22 13:10

hansi


1 Answers

The parameter is actually dataType, not datatype (JavaScript is case sensitive).

You can try with:

dataType: 'json' // not datatype

within your ajaxSetup;

like image 98
thecodeparadox Avatar answered Oct 13 '22 09:10

thecodeparadox