落叶无痕
落叶无痕

Reputation: 29

How to make AJAX responseText Convert to JSON?

var signal=jQuery.ajax({
    url: "http://###/user/checkcaslogin.aspx",
    dataType: "json",
    success: function(data){
    }
});

Object structure

I want to Convert responseTextinto a JSON object, I have tried JSON.parse() but

Syntax error

and typeof signal.responseText //String

Upvotes: 0

Views: 2318

Answers (2)

Quentin
Quentin

Reputation: 943152

If you want to parse the response text as JSON, first you have to send JSON in the response.

JSON Lint is a useful tool for debugging this sort of thing.

This is not valid JSON:

{ "code": 00 }

The Number data type in JSON cannot begin with a double zero.

Numbers in JSON


After you fix the response, the success function will fire, and data will be the result of parsing the JSON.

Upvotes: 6

Robba
Robba

Reputation: 8324

The data parameter is already an object, so you can just use it as object. If you prefer to get the JSON string for whatever reason, use JSON.stringify(data) to get the JSON string.

Upvotes: 0

Related Questions