Meru
Meru

Reputation: 1595

jQuery.parseJSON() no Object / not working

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

Upvotes: 0

Views: 2625

Answers (3)

VenomVendor
VenomVendor

Reputation: 15392

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.

Upvotes: 1

John S
John S

Reputation: 21482

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.

Upvotes: 2

tf.alves
tf.alves

Reputation: 917

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.

Upvotes: 0

Related Questions