0

I am trying to access a value from an JsonObject, retrieved via jqXHR.responseText from the backend which is written in Java.

Data returned as Streaming Output by backend:

...
String msg = "{'msgkey':'my message to the world'}";
return JSON.defaultJSON().forValue(msg);
...

Access via ajax-call, here the done-callback-function:

....
$.ajax({
  type: "GET",
  contentType: "application/json",
  url: url,
  dataType: "json"
}).done(function (data, status, jqXHR) {
var resJson = jqXHR.responseText;
console.log("done jqXHR.responseText " + resJson);
var help = jQuery.parseJSON(resJson);
console.log("done help.status: " + help.status);
....

Result is: help.status undefined.

Why? Is parsing or the '' wrong? I guess I missed to create an object, but I have no clue why it does not work.

I tried the small example, which is on th jQuery-site, which works perfectly fine:

var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" );

Any ideas?

Thanks

10
  • pass dataType: 'json' to the ajax request... then no need to parse the value your self... also write an error handler to see whether there are any parse errors Commented Dec 30, 2013 at 0:51
  • I believe json uses " not '... Commented Dec 30, 2013 at 0:52
  • I have json set as data-type. As for the " and ', I think I tried also this, but will try again. Commented Dec 30, 2013 at 0:55
  • Your backend msg variable is already a string in almost JSON format (it would be JSON if you used the correct quote character as already mentioned), so what is the JSON.defaultJSON().forValue(msg) part supposed to do? Commented Dec 30, 2013 at 0:55
  • What does e.g. firebug tell you is coming over the wire? Have you tried to run it through a json validator (e.g. jsonlint.com)? Commented Dec 30, 2013 at 0:55

3 Answers 3

2

Try the following.

First get the server to return proper JSON (with double quotes):

return "{\"status\":\"searched word not found\"}";

Then use the following on the client side:

.done(function (data) {
    console.log("data.status: " + data.status);
    ...

Since you specify dataType: 'json', jquery will automatically parse the response text into an object, which is the data parameter to the .done() function.

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

1 Comment

Hello! Yes, the escaping I had in before was correct. With escaping the " it works.
1

set datatype to html not json

$.ajax({
  type: "GET",
  contentType: "application/json",
  url: url,
  dataType: "html"
}

EDIT

You have to understand the importance of dataType

I will continue to answer your question "jQuery.parseJSON() not working".

  • Since you have set dataType: "json", you are typing to parse json object.
  • jQuery.parseJSON() accepts string ONLY.
  • You cannot parse JSON for second time.
  • So directly access values by "obj.key".

    var help = jQuery.parseJSON(resJson);
    console.log("done help.status: " + resJson.status);

Continuing with you code.

var help = jQuery.parseJSON(resJson);
console.log("done help.status: " + help.status);
  • you are typing to parse json using jQuery.parseJSON()
  • Set data type to "html" or "text"
  • Now use jQuery.parseJSON().
  • access values by "obj.key" as above.

    check the demo http://jsfiddle.net/VenomVendor/Def7N/ to see the differences. Make sure you open the console before sending request.

6 Comments

Why? He's expecting a json object, isn't he?
@Leo He is expecting json but question is jQuery.parseJSON() is not working, I have updated/improved the answer with demo explaining the difference in dataType
Yes I expected json. Whereas I am totally ok, if I figure out that an error-message as simple String is better, to transfer only a simple string. So far I am returning from the server following "{\"status\":\"word in\"}". The reason I tried to transfer something that can be parsed to json is, that I thought this would be better. But actually I am not sure at all if it is better maybe I just did it because of the Json hype, so to say that I just was starting to learn to use Json. So if you would have any thoughts on that also, please just share them with me (a she ;) btw).
@Meru though you expect json it's good to use html or text then create json object(s) using jQuery.parseJSON() I have updated the demo with try catch. Let me know if have any issues. Happy Coding ;)
Hello! Thanks a lot for the try-catch-reminder. So, it there a } too much at the end of the json? So, those try-catch-blocks are highly needed I guess, aren't they? You think json is good then? So I still would need to change the data-format then I apprehend.
|
0

Your request is already asking for a JSON object:

"json": Evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead.

From: jQuery.ajax()

There is no need to parse it further, as it already is a JSON object.

2 Comments

Thanks for reminding me about the docu. Yes, it says it. It seams I read but acutally did not read. About the part "response as JSON and returns a JavaScript object", this is that the javascript object is the Json object due to Json objects are javascript object. Correct?
Yes, it parses the JSON string and returns a Javascript object!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.