i have a problem on my angular app :
i have a Curl request that work fine in my terminal :
curl -d "grant_type=password&client_id=IDXXXXXX&client_secret=SECRET&username=USERNAME&password=PASSWORD" https://MYINSTANCE/oauth_token.do
now i want to use it with angular JS.
here's what i tried :
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);
});
But the service returns me Error.
I'm a noob on Curl requests with angular, someone can give me some advices?
Thx a lot!
Regards
Your JS is attempting to send a GET request with data in the body. Your curl request is implicitly using POST when you pass data with -d
. Also, by calling stringify
, the data you are sending is a JSON string instead of standard POST format. So to get your JS to match your curl request (if you're also using jQuery):
$http({
method: 'POST',
url: 'https://MYINSTANCE/oauth_token.do',
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
data : $.param(getDatas)
})...
Without jQuery ( $.param
) you can write a generic function to turn an object into a POST data string:
var formData = new FormData();
formData.append("grant_type", "password");
formData.append("client_id", "ID");
...
$http({
...
data: formData
})...
Or build the POST string directly:
data: "grant_type=password&client_id=" + client_id + "&secret=" ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.