jQuery XML REST Access-Control-Allow-Origin

前端 未结 3 1615
孤独总比滥情好
孤独总比滥情好 2020-12-11 03:56

I am calling Version One REST API and keep getting XMLHttpRequest cannot load https://www10.v1host.com/... Origin http://localhost:8080 is not allowed by Access-Contro

相关标签:
3条回答
  • 2020-12-11 04:42

    It is not possible as long as the server of the REST API (not your server) allows the request from a different origin by setting the CORS (Cross-Origin Resource Sharing) HTTP header, for example by setting the "Access-Control-Allow-Origin" HTTP header in the response:

    Access-Control-Allow-Origin: *
    

    or

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

    However what you can do is call your own server with Ajax, then use your own server as a kind of proxy to make a call to the other server in a different domain, parse the XML or HTML result with a suitable parser, and give the result back to the client:

     client --(Ajax)--> server
                        server --(HTTP)---> Site 
                        server <----------
     client <----------
    
    0 讨论(0)
  • 2020-12-11 04:46

    i know it's an old question but i think there is an better answer, via jQuery documentation:

    dataType

    multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

    0 讨论(0)
  • 2020-12-11 04:52

    You can't do a cross domain XMLHttpRequest, period. As for your own parse XML - that would be only possible if you could get an escaped string instead of bare XML from server. There is no magical parse JSON - JSONP technique just requests another script dynamically adding <script src=...> to header, everything inside is treated as normal JavaScript.

    0 讨论(0)
提交回复
热议问题