6
jQuery.ajax({
           type: "GET",
           url: 'http://example.com/restaurant/VeryLogin(username,password)',
           dataType: "json",

           success: function (data) {
               alert(data);
           },
           error: function (XMLHttpRequest, textStatus, errorThrown) {
               alert("error");
           }
       });

it alerts success, but data was null. The url returns xml data, if we specify the dataType we can get the json data,but here it was not getting any data.

Any help appreciated.

Kenster
  • 23,465
  • 21
  • 80
  • 106
tiru
  • 789
  • 5
  • 20
  • 42

4 Answers4

9

Javascript is subject to the same domain policy. This means for security a JS Script in a client browser can only access the same domain as it came from.

JSONP is not subject to the same restrictions.

Check the jQuery docs on JSONP here:

http://api.jquery.com/jQuery.getJSON/

Here is a working example of using JSONP to access a cross-domain service via JQuery AJAX:

http://jsbin.com/idasay/4

And just in case JSBIN deletes this paste in the future:

jQuery.ajax({
     type: "GET",
     url: 'http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username=demo',
     dataType: "jsonp",
     cache: false,
     crossDomain: true,
     processData: true,


     success: function (data) {
         alert(JSON.stringify(data));
     },
     error: function (XMLHttpRequest, textStatus, errorThrown) {
         alert("error");
     }
 });
pmckeown
  • 3,419
  • 3
  • 31
  • 35
  • i have already followed the link,but it didn't produced the result. – tiru Mar 27 '12 at 13:07
  • From the link posted previously: " Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol. Script and JSONP requests are not subject to the same origin policy restrictions." – pmckeown Mar 27 '12 at 13:09
  • I would advise looking at how to achieve what you need using the JSONP example at the above link. – pmckeown Mar 27 '12 at 13:09
  • i tried jsonp also but no result. If i replace an url that produces the json data directly without specifying the content means adding url+&json,i'm getting the response.But not getting any for the xml response. – tiru Mar 27 '12 at 13:15
  • If you request jsonp, you won't get any xml in the response, you'll get JSON. xhr.responseText should contain the returned JSON. – pmckeown Mar 27 '12 at 13:20
2

It's impossible to use Ajax to get cross-domain data directly without changing backend. It's called Same origin policy.

You can set the special header Access-Control-Allow-Origin in backend(how do to this). Or you can use JSONP.

Paul Wheeler
  • 18,988
  • 3
  • 28
  • 41
Lai Yu-Hsuan
  • 27,509
  • 28
  • 97
  • 164
0

here is a fantastic article to make GET and POST cross-domain call : http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

It just helped me a lot....shoot comments for any query.

Savaratkar
  • 1,974
  • 1
  • 24
  • 44
0

Look for jsonp datatype.

jQuery.ajax({
       type: "GET",
       url: 'http://xxx.com/restaurant/VeryLogin(username,password)',
       dataType: "jsonp",
   cache: false,
       crossDomain: true,
   processData: true,

       success: function (data) {
           alert(data);
       },
       error: function (XMLHttpRequest, textStatus, errorThrown) {
           alert("error");
       }
   });
Francis P
  • 13,377
  • 3
  • 27
  • 51