jQuery Cross Domain Ajax jQuery Cross Domain Ajax json json

jQuery Cross Domain Ajax


Looks like the inner JSON struct is passed along as a string. You'll have to JSON.parse() it once more to get that data as an object.

try {  responseData = JSON.parse(responseData);}catch (e) {}

Edit:Try the following:

$.ajax({    type: 'GET',    dataType: "json",    url: "http://someotherdomain.com/service.svc",    success: function (responseData, textStatus, jqXHR) {        console.log("in");        var data = JSON.parse(responseData['AuthenticateUserResult']);        console.log(data);    },    error: function (responseData, textStatus, errorThrown) {        alert('POST failed.');    }});


Unfortunately it seems that this web service returns JSON which contains another JSON - parsing contents of the inner JSON is successful. The solution is ugly but works for me. JSON.parse(...) tries to convert the entire string and fails. Assuming that you always get the answer starting with {"AuthenticateUserResult": and interesting data is after this, try:

$.ajax({    type: 'GET',    dataType: "text",    crossDomain: true,    url: "http://someotherdomain.com/service.svc",    success: function (responseData, textStatus, jqXHR) {        var authResult = JSON.parse(            responseData.replace(                '{"AuthenticateUserResult":"', ''            ).replace('}"}', '}')        );        console.log("in");    },    error: function (responseData, textStatus, errorThrown) {        alert('POST failed.');    }});

It is very important that dataType must be text to prevent auto-parsing of malformed JSON you are receiving from web service.

Basically, I'm wiping out the outer JSON by removing topmost braces and key AuthenticateUserResult along with leading and trailing quotation marks. The result is a well formed JSON, ready for parsing.


The response from server is JSON String format. If the set dataType as 'json' jquery will attempt to use it directly. You need to set dataType as 'text' and then parse it manually.

$.ajax({    type: 'GET',    dataType: "text", // You need to use dataType text else it will try to parse it.    url: "http://someotherdomain.com/service.svc",    success: function (responseData, textStatus, jqXHR) {        console.log("in");        var data = JSON.parse(responseData['AuthenticateUserResult']);        console.log(data);    },    error: function (responseData, textStatus, errorThrown) {        alert('POST failed.');    }});