Reputation: 945
im need to replicate this Jquery ajax functionality:
$.ajax({
type: 'POST',
url: 'https://api.url.gift/api',
data: JSON.stringify({ "user_id": "100006" }),
success: function(result) {
console.log("asd");
console.log(result);
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
},
});
Which works fine, but when I do this in AngularJS:
var myData = JSON.stringify({"user_id": "100006" });
$http({
url: 'https://api.ecosquared.gift/things/cards/received_byme',
method: 'POST',
data: myData,
headers: { 'Content-Type': 'application/json; charset=UTF-8'},
}).success(function(data, status, headers, config){
console.log(data);
}).error(function(data, status, headers, config) {
console.log(data);
});
I get:
XMLHttpRequest cannot load https://api.url.gift/api. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://url.gift' is therefore not allowed access. The response had HTTP status code 405.
One weird thing, if I dont send data parameter, the request goes trough and that error dont raises, but of course the API dont return the data I expect.
Can someone point me in the right direction?
Upvotes: 0
Views: 531
Reputation: 945
Couldn't resolve it with Angular, had to implement NodeJS and do this:
request.post('https://api.url.com/api',
{body: JSON.stringify({"user_id": "222" })}, function (error, response, body) {
console.log(body);
});
And then consume it with AngularJS.
Upvotes: 0
Reputation: 1004
Try sending data without converting it to a JSON string and without the quotations:
var myData = ({user_id: 100006 });
assuming you user_id is a integer
Upvotes: 1