jQuery的阿贾克斯后说XXX不受访问控制 - 允许 - 原产地允许 [英] jquery ajax post says xxx not allowed by Access-Control-Allow-Origin

查看:149
本文介绍了jQuery的阿贾克斯后说XXX不受访问控制 - 允许 - 原产地允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用使用AJAX(要求)易趣FindProducts API,但被卡在以下错误:

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

XMLHtt prequest无法加载 http://open.api.ebay.com /购物?callname = FindProducts 。产地 http://localhost.com/test.php 不受访问控制 - 允许 - 原产地允许

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.

我的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.

我试着设置数据类型:JSONP (我知道XML被检索,但要解决我将它设置为JSONP错误)。它的工作原理,但jQuery的无法解析XML为JSON响应预期。

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 <一href="http://developer.ebay.com/devzone/shopping/docs/concepts/ShoppingAPI_FormatOverview.html#URLExamples">docs

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

更新 工作的例子的。我所做的就是,我改为数据类型:JSONP添加 JSONP:callbackname。因为jQuery默认情况下调用回调参数回调,但eBay的期望它被称为 callbackname 。什么,你需要做的是添加参数的数据地图。请确保你使用正确的参数名称,检查文档,并使用URL方法不是头的方法。希望这有助于。

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.

这篇关于jQuery的阿贾克斯后说XXX不受访问控制 - 允许 - 原产地允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆