5

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?

2
  • 2
    JavaScript is case sensitive. dataType !== datatype. Commented Jun 13, 2012 at 15:33
  • Thanks much, I probably wouldn't have figured it out in quiet some time.. Commented Jun 13, 2012 at 16:10

3 Answers 3

5

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

You can try with:

dataType: 'json' // not datatype

within your ajaxSetup;

Sign up to request clarification or add additional context in comments.

Comments

0

Your response:

"aStringsData" 

is not valid JSON I believe, as JSON supposed to start with a {, right?.

You might mean

{
 "string": "data"
}

3 Comments

Well, it can be either an object, or a list of things (like a string), but not a string in itself, can it? (of course, I can be mistaken, but the first 3 validators I tried same to say the same)
@matt : the json page you link has 2 bolletpoints: they tell that it's an object or a list. There is a definition of "value" later on, sure, but it doens't say the value alone is valid JSON as I read it.
@Nanne: I'm inclined to agree with you. I tried JSON.parse('"foo"') and it worked fine, but the RFC I read said that JavaScript parsers could accept non-JSON as an extension if they wanted... json.org doesn't do a very good job of explaining it, one way or another.
-1

you can try to use something like this:

eval(aString)

1 Comment

This doesn't come close to answering the question. The question is asking why the JSON isn't parsed automatically, not how to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.