0

This below code is for calling a Login rest api through jquery ajax but it returns 403 error. I want to know if I missed any necessary parameter in ajax call or it's server side error. That source(HTML file) and destination(API) both in same cloud server.

But the api works fine in postman without any error

$.ajax({
  type: "POST",
  url: 'https://mzzzzcloudx.am.co.in/login',
  dataType: 'json',
  data: {
    "login": "abcd@gmail.com",
    "password": "12345"
  }, //{"login":$("#login").val(), "password":$("#pass").val()},
  contentType: "application/json",
  headers: {
    "Content-type": "application/json",
    "Access-Control-Allow-Origin": "*"
  },
  success: function(data) {
    console.log(data);
  },
  error: function(err) {
    alert(err); //returns 403 error
    console.log(err);
  }
});

enter image description here

gokila
  • 11
  • 1
  • 1
    The AJAX request itself looks fine, although the `Access-Control-Allow-Origin` header can be removed as it should be in the response from the server, not the request from the client. If this isn't working for you then there's an implementation problem between your request and what the server expects. As such, we can't help with that – Rory McCrossan Apr 28 '20 at 13:25
  • 1
    Could you try to stringify the data `data: JSON.stringify(your-data)`? – Widada Apr 28 '20 at 13:36
  • The problem seems to be server-side... Could be the data sent does not use the correct property names? – Salketer Apr 28 '20 at 14:37
  • I tried JSON.stringify but getting same error. How I make sure that it is a server side issue? – gokila Apr 29 '20 at 05:59
  • Please post a screenshot of your Postman call. – Evan Knowles Apr 29 '20 at 05:59
  • You definitely need to use `JSON.stringify()` on your data. See [How can I use JQuery to post JSON data?](https://stackoverflow.com/questions/6255344/how-can-i-use-jquery-to-post-json-data). I bet the 403 is in response to a pre-flight `OPTIONS` request (look in your browser's _Network_ console). The server is probably not configured for CORS access – Phil Apr 29 '20 at 06:16
  • @Phil So you are saying that the problem is in the server side? and this login API can accessible from Android and IOS App. – gokila Apr 29 '20 at 06:39
  • There was an error connecting to https://mzzzzcloudx.am.co.in/login. when trying through postman. Is server is down ? – Hammad Shabbir Apr 29 '20 at 08:55
  • @Muhammad Sorry I didn't give the original URL. But for original URL am getting correct response in postman. – gokila Apr 29 '20 at 10:46

1 Answers1

-1

Do you try to remove the quote in the field names (login, password) of the data section?

data: {
login: "abcd@gmail.com",
password: "12345"}

And also, do you confirm that the field names (login, password) are the same in the REST method login()?

Shawn Xiao
  • 560
  • 5
  • 18
  • I tried remove the quote in the field names (login, password). But again I face the same issue (getting 403 error). – gokila Apr 29 '20 at 06:42
  • Sorry. The 403 error means that you have no authorization to access the REST service. Could you please check the authorization of Service side? E.g.: Anonymous access. – Shawn Xiao Apr 29 '20 at 07:43
  • Please refer: https://answers.microsoft.com/en-us/ie/forum/ie9-windows_other/what-is-http-403-error/2671a355-3d77-4d04-aa19-b6d353d7aa38 – Shawn Xiao Apr 29 '20 at 07:49