mdv
mdv

Reputation: 945

AngularJS / jQuery sending POST data

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.

Upvotes: 0

Views: 92

Answers (2)

jbigman
jbigman

Reputation: 1270

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;
});

Upvotes: 2

Calvintwr
Calvintwr

Reputation: 8798

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);
    },
 });

Upvotes: 2

Related Questions