简体   繁体   English

如何在Jquery Ajax中使用Json数据

[英]How to consume Json Data in Jquery Ajax

I have a URL, which gives response on browser: https://api.sandbox.paypal.com/retail/merchant/v1/locations 我有一个URL,可在浏览器上提供响应: https : //api.sandbox.paypal.com/retail/merchant/v1/locations

It gives: 它给:

{
    "errorCode": 600031,
    "message": "Missing access token",
    "developerMessage": "You must provide an access token when calling this API. It can be passed as either a header of the form \"Authorization: Bearer \" or as a query parameter called access_token.",
    "errorType": "oauth/missing_access_token",
    "correlationId": "4de95cd8aa090"
}

I tried this: 我尝试了这个:

$.ajax({
    url: "https://api.sandbox.paypal.com/retail/merchant/v1/locations",
    dataType: 'json',
    type: 'POST',
    success: function (data) {

        console.log(data); 
        alert("success", data);
    },
    error: function (data) {
        alert("fail", data);
        console.log(data);
        alert("Sorry..Please try again later"); 
    },
});

But I am not getting the same response as I am getting on browser. 但是我得到的响应与浏览器的响应不同。 I am getting error. 我出错了。

Please check here http://jsfiddle.net/ajitksharma/wehGy/ 请在这里检查http://jsfiddle.net/ajitksharma/wehGy/

However while debugging on Browser console I got the error: 但是,在浏览器控制台上调试时,出现错误:

XMLHttpRequest cannot load https://api.sandbox.paypal.com/retail/merchant/v1/locations . XMLHttpRequest无法加载https://api.sandbox.paypal.com/retail/merchant/v1/locations No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin 'null' is therefore not allowed access. 因此,不允许访问原始“空”。

You can make AJAX calls to a backend API which is on another domain, however, it needs to return JSONP format and not just JSON, otherwise you get and error. 您可以对另一个域上的后端API进行AJAX调用,但是,它需要返回JSONP格式,而不仅仅是JSON,否则会出错。 This is due to same origin policy: https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy . 这是由于相同的来源政策: https : //developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

This discussion may be helpful to understand JSONP: Can anyone explain what JSONP is, in layman terms? 该讨论可能对理解JSONP有所帮助: 有人可以用通俗易懂的方式解释JSONP是什么吗?

Since you don't have control over PayPal's API and you can't ask them to return JSONP to you, these requests to PayPal's API need to be done from the server-side script of your application. 由于您无法控制PayPal的API,并且不能要求他们将JSONP返回给您,因此,对PayPal的API的这些请求需要从应用程序的服务器端脚本中完成。

Running this from any other location, for example JSFiddle, will give you the Access-Control-Allow-Origin error since you're making a cross-domain request. 从其他任何位置(例如JSFiddle)运行此命令,由于发出跨域请求,因此会出现Access-Control-Allow-Origin错误。 Please read further about the same-origin policy . 请进一步了解同源政策

As for the first error, its because your request needs an API key from paypal. 至于第一个错误,是因为您的请求需要来自Paypal的API密钥。 See this page about getting an API key and making a simple request. 有关获取API密钥和进行简单请求的信息,请参见此页面

As Lisa Stoz and kaminari suggested it is not possible to call a service in another domain without some patch work. 正如Lisa Stoz和kaminari所建议的那样,没有一些补丁工作就不可能在另一个域中调用服务。 Now as you see the response when you hit that url via browser it says you need to add an extra header to your ajax request something like 'authorisation' 现在,当您通过浏览器访问该URL时看到响应,它说您需要在ajax请求中添加一个额外的标头,例如“授权”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM