我正在使用 AngularJS 从 Twitter Digits 检索用户帐户数据。
Digits.logIn()
.done(onLogin)
.fail(onLoginFailure);
function onLogin(loginResponse) {
console.log('Digits login succeeded.');
var oAuthHeaders = parseOAuthHeaders(loginResponse.oauth_echo_headers);
apiURL = oAuthHeaders.apiUrl;
credentials = oAuthHeaders.credentials;
$http.get(apiURL,{
headers: {'Authorization': credentials}
}).then(function(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response);
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(response);
});
}
执行get请求时出现错误:
“XMLHttpRequest 无法加载 https://api.digits.com/1.1/sdk/account.json 。请求的资源上不存在 'Access-Control-Allow-Origin' header 。因此不允许访问 Origin ' http://localhost:8000 '。响应的 HTTP 状态代码为 400”。
我已经尝试更改 $httpProvider 配置来修改 header ,但没有成功。
Digits 路由不接受 JSONP 请求。
如何使用 AngularJS 对 Twitter Digits API 执行此 get 请求?
最佳答案
根据文档和示例,您必须对自己的服务器端点进行异步调用,并且该端点应该执行此操作并发回用户数据。
https://github.com/twitterdev/cannonball-web/blob/master/public/javascripts/cannonball.js
说
/**
* Handle the login once the user has completed the sign in with Digits.
* We must POST these headers to the server to safely invoke the Digits API
* and get the logged-in user's data.
*/
关于javascript - Twitter Digits - 使用 AngularJS 检索用户帐户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32737331/