将cURL转换为angularjs $ http
我刚开始学习angularjs,并试图从api获取令牌。当我在终端中使用curl命令时,curl命令正常工作,并且可以获得令牌。虽然我使用$ http很难使其工作。
这里是我的cURL命令:
curl --data "grant_type=authorization_code&client_id=CLIENT_ID&client_secret=SECRET_KEY&redirect_uri=http://localhost:9999&code=AUTH_CODE" https://www.twitchalerts.com/api/v1.0/token
谁能帮助我将此转换使用$ HTTP
试试这个:
var data = {
grant_type :'authorization_code',
client_id :'CLIENT_ID',
client_secret :'SECRET_KEY',
redirect_uri :'http://localhost:9999&code=AUTH_CODE'
};
// Simple GET request example:
$http({
method: 'GET',//or POST
url: 'https://www.twitchalerts.com/api/v1.0/token',
data:data,
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
如果没有再尝试这种
....
data:data,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
....
欲了解更多信息关于角$ http https://docs.angularjs.org/api/ng/service/ $ http
数据和params不能互换GET和POST GET没有contentType – charlietfl
@charlietfl当你提交whith方法GET时,enctype =“application/x-www-form-urlencoded | multipart/form-data | text/plain”,你在想什么,他们发送了哪个头文件? :) –
那么GET没有请求体,所以不需要内容类型。所有的数据都在url中 – charlietfl
那么'$ http'代码试过了。你的错误处理程序告诉你什么?试图提出请求时,您在浏览器控制台中看到什么? – charlietfl
只是它有客户端密钥的事实表明你可能无法与ajax做到这一点,因为它意味着暴露你的凭据在浏览器中 – charlietfl