JQuery XML REST Access-Control-Allow-Origin

I call the Version 1 REST API and continue to receive the XMLHttpRequest cannot load https://www10.v1host.com/... Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin. error XMLHttpRequest cannot load https://www10.v1host.com/... Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin. javascript. I am sending a GET request and it is expected that the XML text will be returned in response. This is my jQuery code:

 $.ajax({ dataType: "xml", url: "https://www10.v1host.com/...", success: function(data, status, xhr) { alert("Load was performed."); } }); 

I changed dataType: "jsonp ", and with this I get the answer in this format:

 jsonp1294354293197(<?xml version="1.0" encoding="UTF-8"?><Asset ...</Asset>) 

But get another javascript error: " Uncaught SyntaxError: Unexpected token < "

Is there a way to get cross-domain XML data data (something like XMLp) or make a workaround for jsonp (prevent JSON format parsing and use native XML analysis)?

+7
source share
3 answers

You cannot run the cross-domain XMLHttpRequest, period. As for your own XML parsing, this would only be possible if you could get the escaped string instead of the bare XML from the server. There is no magic analysis. The JSON-JSONP method simply requires the dynamic addition of <script src=...> to << β†’>, everything inside is treated like normal JavaScript.

+5
source

This is not possible if the REST API server (and not your server) allows you to request a different origin by setting the CORS (Cross- Source Share) HTTP header, for example, setting the HTTP header "Access-Control-Allow-Origin" in the response:

 Access-Control-Allow-Origin: * 

or

 Access-Control-Allow-Origin: http://localhost:8080 

However, you can call your own server using Ajax, and then use your own server as a kind of proxy to call another server in another domain, parse the XML or HTML result using a suitable parser, and return the result to the client:

  client --(Ajax)--> server server --(HTTP)---> Site server <---------- client <---------- 
+5
source

I know this is an old question, but I think there is a better answer, jQuery documentation :

Datatype

several values ​​separated by spaces: with jQuery 1.5, jQuery can convert dataType from what it got in the Content-Type header to what you need. For example, if you want the text response to be processed as XML, use "text xml" for dataType. You can also make a JSONP request if it received text and interpreted jQuery as XML: "jsonp text xml." Similarly, a string string such as "jsonp xml" will first try to convert from jsonp to xml, and, otherwise, convert from jsonp to text, and then from text to xml.

0
source

All Articles