简体   繁体   中英

JQuery Cross Domain Error - No 'Access-Control-Allow-Origin'

The POST request

var url = "http://xxx.xxx.x.x/MyServices.svc/GetOrders";
$.ajax({
    type: "POST",
    url: url,
    crossDomain : true,
    data: 'abcd',
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (result) {
        data = result.data;                             
    },
    error: function (xhr, ajaxOptions, thrownError) {
    debugger;
        alert('error');
    }
});

When the url address is localhost i am getting a response When trying to do so across servers I am getting an Error :

405 (Method Not Allowed)

XMLHttpRequest cannot load http://xxx.xxx.xx/MyServices.svc/GetOrders . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

All of the answers on the web where inconclusive.

If you can deal with json in return, then try using jsonp for speaking between domains:

$.ajax({
  type: "POST",
  dataType: 'jsonp',
  ...... etc ....

As documented in official site :-

The advent of JSONP — essentially a consensual cross-site scripting hack — has opened the door to powerful mashups of content. Many prominent sites provide JSONP services, allowing you access to their content via a predefined API.

and

jQuery handles all the complex aspects of JSONP behind-the-scenes — all we have to do is tell jQuery the name of the JSONP callback parameter specified by YQL ("callback" in this case), and otherwise the whole process looks and feels like a normal Ajax request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM