在新浪微博快速登录时,获得code之后如何发送请求获得token?
已经取得了code,整理后的url如下:
var url = 'https://api.weibo.com/oauth2/access_token?client_id='+CONFIG['wb_appid']+'&client_secret='+CONFIG['wb_secret']+'&grant_type=authorization_code&redirect_uri='+getCurrLocation+'&code='+obj[key];
根据http://open.weibo.com/wiki/Oauth2/access_token所说,请求需要post方式。做了两次试验代码如下(不考虑兼容性):
第一个:在index.html文件中加入了
<meta http-equiv="Access-Control-Allow-Origin" content="*" />
var xhr = new XMLHttpRequest;
xhr.open("post", url , true);
xhr.setRequestHeader("Content-Type", "text/html;charset=utf-8");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var text = xhr.responseText;
var ary = JSON.parse(text);
console.log(ary);
}
};
xhr.send();
报错:
POST https://api.weibo.com/oauth2/access_token $.ajax @ zepto.extend.js:1546(anonymous function)
index.html?code=504515c6743499c94141d66191ea0f11:1 XMLHttpRequest cannot load https://api.weibo.com/oauth2/access_token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 401.
第二个:
$.ajax({
type: "post",
url: "https://api.weibo.com/oauth2/access_token",
data: obj,
dataType: "jsonp crossdomain:true",
success: function(data){
console.log(data);
},
error:function(data){
console.log(data);
}
});
其中obj
var obj ={
grant_type:'authorization_code',
client_id:CONFIG['wb_appid'],
client_secret:CONFIG['wb_secret'],
redirect_uri:getCurrLocation(),
code:obj[key]
};
报错:XMLHttpRequest cannot load https://api.weibo.com/oauth2/access_token?client_id=1234567890&client_secre…0;%20%20%20%20%20%20%20%20%20%20%20}&code=9000d5a0f26de70042fbdaf465653221f1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
请大神们指导一下,该如何做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你解决了么?
同求 这个接口我直接arc访问了确实是要走post 走get返回不支持get方法
试过了下别人说的jsonp跨域 但是 jsonp不支持post
解决了吗 ,大神?
请问你解决了吗?我也在做这块 接受状态对着呢 就是收不到返回值?