0

I need to send data to an API that (for some weird reason) expects this {'user_uid: '22222'}, literally.

Im trying to do it with jQuery like this:

 $.ajax({
    type: 'POST',
    url: 'https://api.com/api/',
    'user_id': '2222'

    success: function(result) {
       console.log(result);
       if(result.error){

       }else{

       }
    },

    error: function(xhr, ajaxOptions, thrownError) {
       console.log(xhr.status);
       console.log(thrownError);
    },
 });

And with AngularJS:

 $http.post('https://api.com/api', {'user_id': '2222'})
 .success(function(data) {

}

Weird thing, if I try to do it in Postman, I need to use 'raw' query and enter: {'user_id': '22222'}

But if I use form-data it fails

Can some point me in the right direction?

Thanks in advance.

2
  • 1
    'user_id': '2222' <-- invalid statement Commented May 4, 2015 at 14:25
  • Your jQuery example is going to throw a syntax error regardless. You should double check the documentation on how create AJAX calls. If you need a literal string of characters, you should specify plain text. Likewise, if you're trying to send JSON data, you should stringify it properly, and again specify your data type. Commented May 4, 2015 at 14:28

2 Answers 2

2

I would have inserted this in the comment but it would be difficult to read the code.

I am suspecting that the API needs a stringified input. Can you try:

$.ajax({
    type: 'POST',
    url: 'https://api.com/api/',
    data: JSON.stringify({'user_id': '2222'}),

    success: function(result) {
       console.log(result);
       if(result.error) {

       } else {

       }
    },

    error: function(xhr, ajaxOptions, thrownError) {
       console.log(xhr.status);
       console.log(thrownError);
    },
 });
Sign up to request clarification or add additional context in comments.

3 Comments

tried: data: JSON.stringify({'user_id': '222222'}), same error :(
Hi there, worked with this: JSON.stringify({ "user_id": "100006" } how can I emulate that in AngularJS
2

Try this :

$http({
  url: 'customUrl',
  method: 'POST',
  data: JSON.stringify({ "user_id": "100006" }),
  headers: { 
    'Content-Type': 'application/json; charset=UTF-8'
  }
}).then(function (success) {
  return success;
}).catch(function (e) {
  throw e;
});

7 Comments

Im trying: $http.post('api.urlrl.giftapi', {"user_id": "2222" }) but it says: o 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. This is working with Jquery ajax call
Access-Control-Allow-Origin is a CORS (Cross-Origin Resource Sharing) header. You should read more about CORS.
Same behaviour described here : forum.ionicframework.com/t/…
CORS is enabled server-side, header: Access-Control-Allow-Origin: * And Jquery works with the same URL, dont know why Angular doesen't
Unknown data type may raise CORS issues. Try to force jsonp in the angular call ? (The ajax call may put an implicit data type.)
|

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.