繁体   English   中英

带有jquery ajax的POST请求

[英]POST request with jquery ajax

llo我正在使用box api进行集成,他们给了curl以获得访问令牌,如下所示

is curl https://www.box.com/api/oauth2/token \
-d 'grant_type=authorization_code&code={your_code}&client_id={your_client_id}&client_secret={your_client_secret}' \
-X POST

我想使用jquery ajax请求获取访问令牌,我已经厌倦了如下代码:

var data2= '{"grant_type":"authorization_code", "client_id":"61oliuyi1jckqos0j8zzl9h4t791umux" ,"client_secret":"Oi1XS56al61oOrrvAL3i5gdPTfe7QO9V" ,"code" :"'+code+'"}';
var data1 = JSON.parse(data2);

$.ajax({
                    type: 'POST',
                    url:  "https://www.box.com/api/oauth2/token",
                    data: data1,
                    success: function(json) {
                        console.log(e);
                       alert("success"+json);
                    },
                    error: function(e) {
                        console.log(e)
                        alert("Failure"+ JSON.stringify(e));

                    }
                });

MLHttpRequest cannot load https://www.box.com/api/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

问题仅仅是由于跨域请求。 Javascript Origin Policy将阻止您从另一个域 (例如您正在使用的localhost) 向API发出Ajax请求 这里的错误很好的解释。

在尝试使用Box.com API时,应使用YQL来使代码正常工作。 他们博客中的这篇开发文章准确地显示了如何集成YQL,这似乎是最好的选择。

尝试这个:

$.ajax({
                    type: 'POST',
                    url:  "https://www.box.com/api/oauth2/token",
                    data: data1,
                    dataType:"json",
                    success: function(val) {
                        console.log(e);
                       alert("success"+val);
                    },
                    error: function(e) {
                        console.log(e)
                        alert("Failure"+ JSON.stringify(e));

                    }
                });

暂无
暂无

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

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