jquery ajax post说xxx不允许通过Access

编程入门 行业动态 更新时间:2024-10-28 12:22:38
jquery ajax post说xxx不允许通过Access-Control-Allow-Origin(jquery ajax post says xxx not allowed by Access-Control-Allow-Origin)

我正尝试使用AJAX调用eBay FindProducts API( post请求),但遇到以下错误:

XMLHttpRequest无法加载http://open.api.ebay.com/shopping?callname=FindProducts 。 Access-Control-Allow-Origin不允许来源http://localhost.com/test.php 。

我的代码:

$.ajax ({ type: "POST", url: 'http://open.api.ebay.com/shopping?callname=FindProducts', dataType: ($.browser.msie) ? "text" : "xml", contentType: 'application/x-javascript', crossDomain : true, data: { 'X-EBAY-API-APP-ID' : 'ebayAppId', 'X-EBAY-API-VERSION': '771', 'X-EBAY-API-SITEID': '0', 'X-EBAY-API-REQUEST-ENCODING': 'NV', 'X-EBAY-API-RESPONSE-ENCODING': 'json', 'QueryKeywords' : '753759971632', 'MaxEntries' : '3' }, success: function (result) { alert('success'); alert(result); }, error: function (data) { alert((data)); } })

我怎样才能解决这个错误。

我试着设置dataType : jsonp (我知道XML正在被检索,但要解决我将它设置为jsonP的错误)。 它的工作原理,但jQuery无法解析XML作为JSON响应的预期。

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

XMLHttpRequest cannot load http://open.api.ebay.com/shopping?callname=FindProducts. Origin http://localhost.com/test.php is not allowed by Access-Control-Allow-Origin.

My code:

$.ajax ({ type: "POST", url: 'http://open.api.ebay.com/shopping?callname=FindProducts', dataType: ($.browser.msie) ? "text" : "xml", contentType: 'application/x-javascript', crossDomain : true, data: { 'X-EBAY-API-APP-ID' : 'ebayAppId', 'X-EBAY-API-VERSION': '771', 'X-EBAY-API-SITEID': '0', 'X-EBAY-API-REQUEST-ENCODING': 'NV', 'X-EBAY-API-RESPONSE-ENCODING': 'json', 'QueryKeywords' : '753759971632', 'MaxEntries' : '3' }, success: function (result) { alert('success'); alert(result); }, error: function (data) { alert((data)); } })

How can I get through this error.

I tried setting dataType : jsonp (I know XML is being retrieved, but to workaround the error I set it to jsonP). It works but jQuery was unable to parse the XML as the json response was expected.

最满意答案

如果您在您的网址中添加&responseencoding=JSON ,则会根据文档获得JSON响应

更新 工作示例 。 我所做的是我更改为dataType:'jsonp'添加了jsonp:'callbackname' 。 因为jQuery默认调用回调参数回调函数,但eBay期望它被称为callbackname 。 你必须做的是将你的参数添加到数据图。 确保使用正确的参数名称,检查文档并使用URL方法而不是标题方法。 希望这可以帮助。

If you add &responseencoding=JSON to your URL you will get the response as JSON according to docs

UPDATE Working example. What I've done is that I changed to dataType:'jsonp' added jsonp:'callbackname'. Because jQuery by default calls the callback parameter callback, but eBay expect it to be called callbackname. What you have to do is add your parameters to the data map. Make sure you use the correct parameter names, check the docs and use the URL method not the header method. Hope this helps.

更多推荐

XML,error,FindProducts,dataType,电脑培训,计算机培训,IT培训"/> <meta name=&qu

本文发布于:2023-08-04 22:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不允许   ajax   jquery   post   Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!