The jQuery AJAX state is "200 OK" but there is no data response

JQuery:

$.ajax({ url : url, type : 'GET', dataType: 'json', data: { 'FN' : 'GetPages', 'PIN' : '7659' }, xhrFields: { withCredentials: true }, crossDomain: true, success: function(data) { alert('succsess'); console.log('data', data); }, error: function (xhr, ajaxOptions, thrownError) { alert('error'); console.log(xhr.status); console.log(thrownError); } }); 

Firebug firefox network

Firebug error

What's happening

The AJAX error: event fires and my console.log output is:

xhr.status → 0

thrownError → (empty String)

This is normal? When I type the URL in the browser, I get the file with the JSON content, this should not be a problem, right?

+6
source share
3 answers

Thanks @CrimsonChin, I know his problem with the same source policy

In computing, the same origin policy is an important security concept for a number of browser-based programming languages, such as JavaScript. This policy allows scripts to run on pages coming from the same site to access other methods and properties using no specific restrictions, but it prevents access to most methods and properties from pages on different sites. [1]

(from http://en.wikipedia.org/wiki/Same_origin_policy )

Giving JavaScript clients basic access to your resources simply requires the addition of a single HTTP response header, namely:

 Access-Control-Allow-Origin: * Access-Control-Allow-Origin: http://foo.example.com 

(from http://enable-cors.org/ )

Ofc, including a JSON response in a JSONP response will also work. thanks @djakapm

+10
source

Try this one

 $.ajax({ type : "GET", url : URL, data: { 'FN' : 'GetPages', 'PIN' : '7659' }, xhrFields: { withCredentials: true }, crossDomain: true, dataType : "jsonp", jsonp : "jsoncallback", jsonpCallback : "SMS", cache : true, success : function(service_data) { }, error : function(msg) { alert(JSON.stringify(msg)); } }); 
+3
source

I cannot understand from the image, but if you are trying to send an AJAX request to another domain, you need to use JSONP , a simple AJAX request will not be enough

Try changing dataType: 'json' to dataType: 'jsonp'

and add callback=? to your URL

+2
source

All Articles