jquery ajax post says xxx not allowed by Access-Control-Allow-Origin

后端 未结 3 1683
难免孤独
难免孤独 2021-01-14 16:43

I was trying to call eBay FindProducts API using AJAX (post request) but was stuck at the following error:

XMLHttpRequest cannot load htt

3条回答
  •  广开言路
    2021-01-14 17:06

    If you read the jQuery.ajax() documentation, you can use jsonp and still have a different return type to be parsed.

    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.

    So basically all you have to do is change your dataType line of code to this:

    dataType: ($.browser.msie) ? "jsonp text xml" : "xml",
    

    Or you can add a &responseencoding=JSON parameter to your URL, as stated in another answer.

提交回复
热议问题