Cross domain ajax request returning XML: Origin ht

2019-08-05 18:55发布

问题:

This is the piece of jQuery that's not working for me :

$.ajax({
  crossDomain: true,
  type: "POST",
  url: "https://www.testdatasolutions.com/reportgw",

  data: "ACCOUNT=creditreport123&PASSWD=asdj97sdf&PASS=2&PROCESS=PCCREDIT&NAME=Robert+Ice&SSN=891-42-3221&ADDRESS=111+W+8th+St&CITY=Fantasy+Island&STATE=IL&ZIP=60750&BUREAU=TU&PRODUCT=CREDIT&DEFAULTOUTPUT=XML"
})

 .done(function( msg ) {
  alert( "Data Saved: " + msg );
});

The output error says:

XMLHttpRequest cannot load https://www.testdatasolutions.com/reportgw. Origin http://pmr.techforge.us is not allowed by Access-Control-Allow-Origin.

A similar topic can be found here, but it covers only the cases where output is in JSON.

So my question is, is it possible to handle cross domain ajax requests returning XML, or must I absolutely use JSONP?

回答1:

It must be JSONP, XML is not allowed for cross-domain requests by default.

However, with a little server-side programing you could create a proxy and load the data through curl or similar on the server side within your own domain, and output it as XML. That way you can access an url within your own domain instead and you won't have to do a cross-domain request from the client, it is handled "behind the scenes".



回答2:

You can try with http://enable-cors.org/. You can check the list of browsers that support CORS at http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing#Browser_support.