-1

I am trying to make an API call in Ajax, here it is :

    $.ajax({
    method: "POST",
    url: "https://login.salesforce.com/services/oauth2/token",
    crossDomain: true,
    data: "grant_type=password" +
      "&client_id=" + CLIENTID +
      "&client_secret=" + CLIENTSECRET +
      "&username=" + USERNAME +
      "&password=" + PASSWORD,
    dataType: 'jsonp',
    success: function (data) {
        console.log(data);
    },
    error: function (err) {
        console.log(err);
    }
});

But when I try it, I got a console error :

jquery.min.js:2 GET https://login.salesforce.com/services/oauth2/token?callback=jQuery33100... net::ERR_ABORTED 400 (Bad Request)

What am I doing wrong? I tried in postman with the same info and i got a status 200

Andrea
  • 145
  • 9
  • oh, wait .... JSONP means that the request will be a `GET` - because that's how JSONP works - so, you're definitely not `POST`ing (check the browser console for the METHOD of the request) – Jaromanda X Sep 20 '18 at 10:00
  • I am not sure he understand it, can i supress it? – Andrea Sep 20 '18 at 10:02
  • For the quote, it was when i put it in stackoverflow, sorry i edited it – Andrea Sep 20 '18 at 10:03

1 Answers1

0

I did a simple post with postman and it brought me back invalid client_id(which is correct since i didnt specify it).

Here is my post: https://login.salesforce.com/services/oauth2/token?grant_type=password

Try sending only grant_type=password and see what happens. If it doesnot return bad request, then something should be wrong with the parameters. If it returns bad request then you are doing something wrong with you post syntax.

Hope it helps give you a direction

  • I tried with only grant_type password but get the exact error, like it was said in the comments maybe it comes from the callback=jQuery... – Andrea Sep 20 '18 at 10:06
  • `dataType: 'jsonp',` effectively changes the request to a GET - see https://stackoverflow.com/questions/4508198/how-to-use-type-post-in-jsonp-ajax-call – Jaromanda X Sep 20 '18 at 10:19
  • Oh i see, thanks a lot, do you think there is a way to make a cross-domain request without the dataType: 'jsonp'? – Andrea Sep 20 '18 at 10:26
  • yes - there are several ways - one depends on the server allowing it, another is using your own server to "proxy" the request – Jaromanda X Sep 20 '18 at 10:28
  • Oh thank you, i will search for your second proposition :) – Andrea Sep 20 '18 at 11:39