2013-12-17 199 views
2

LLO我使用箱API来整合他们给刚刚卷曲获得访问令牌,这是遵循POST请求

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. 
+0

也请把你的问题是什么,你上哪儿去缩小它。 –

+0

MLHttpRequest无法加载https://www.box.com/api/oauth2/token。请求的资源上没有“Access-Control-Allow-Origin”标题。原因'http:// localhost:8080'因此不被允许访问。 – user3110960

回答

2

问题是由于跨域请求。 Javascript起源策略将阻止您从另一个域(例如您正在使用的localhost)向API发出Ajax请求。尼斯错误here的解释。

正如您试图使用Box.com API,您应该使用YQL来使您的代码正常工作。 This从他们的博客开发文章显示如何整合YQL,似乎是最好的选择。

+0

什么是解决方案然后 – user3110960

+0

使用YQL - > http://developer.yahoo.com/yql/。代码在我上面链接的文章中描述。 –

+0

或者,使用一些服务器端逻辑与服务进行交互(在rails上可能有其他的libs和ruby) – Slicedpan

1

试试这个:

$.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)); 

        } 
       }); 
+0

由于Web浏览器执行的通用来源策略,这仍然会失败 – Slicedpan