[英]Angular Js Curl -d http request
我的角度應用程序有問題:
我有一個在我的終端中可以正常工作的Curl請求:
curl -d "grant_type=password&client_id=IDXXXXXX&client_secret=SECRET&username=USERNAME&password=PASSWORD" https://MYINSTANCE/oauth_token.do
現在我想將其與Angular JS一起使用。
這是我嘗試的:
var getDatas = {
grant_type: "password",
client_id: "ID",
client_secret : "PASS",
username : "USER",
password : "PASSWORD"
}
$http({
method: 'GET',
url: 'https://MYINSTANCE/oauth_token.do',
data : JSON.stringify(getDatas)
}).then(function successCallback(response) {
alert("response : " + response);
}, function errorCallback(response) {
alert("error : " + response);
});
但是服務返回我錯誤。
我對帶有角的Curl請求不滿意,有人可以給我一些建議嗎?
多謝!
問候
您的JS嘗試發送帶有正文中數據的GET請求。 當您使用-d
傳遞數據時,您的curl請求將隱式使用POST。 另外,通過調用stringify
,您發送的數據是JSON字符串而不是標准POST格式。 因此,要使您的JS匹配您的curl請求(如果您還使用jQuery):
$http({
method: 'POST',
url: 'https://MYINSTANCE/oauth_token.do',
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
data : $.param(getDatas)
})...
如果沒有jQuery( $.param
),則可以編寫一個通用函數將對象轉換為POST數據字符串:
var formData = new FormData();
formData.append("grant_type", "password");
formData.append("client_id", "ID");
...
$http({
...
data: formData
})...
或直接構建POST字符串:
data: "grant_type=password&client_id=" + client_id + "&secret=" ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.