0

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?

8
  • 1
    Simple, this is a CORS request which the server specifically has to enable for you by adding the aforementioned header. If you don't have access to the servercode take a look at en.wikipedia.org/wiki/JSONP Commented May 4, 2015 at 16:19
  • Server headers are in place, post request returns this: Access-Control-Allow-Origin: * . As I mentioned, the SAME Jquery ajax request works fine, but Angular doesen't Commented May 4, 2015 at 16:20
  • Could you copy paste the full response? Commented May 4, 2015 at 16:21
  • The question already have the full browser response. Commented May 4, 2015 at 16:24
  • More important would be the request headers being sent. Commented May 4, 2015 at 16:26

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Same error, on Jquery had to convert the data to string to get it working
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.