1

I am using cordova and while doing json i am getting an error "Failed to load resource: the server responded with a status of 400 (Bad Request)".

enter image description here

But the same code when I run it on postman is getting the right answer.Please help me to solve this problem. The code is:

$.ajax({
  url: url,
  type: "POST",
  async: false,
  ContentType: "application/json; charset=utf-8",
  data: jData,
  dataType: "json",

  success: function(response) {
    console.log(response)

  },
  error: function(jqXHR, textStatus, errorThrown) {


  },

});

And a screenshot of the right answer on the postman is also given for your reference

Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
Durga
  • 11
  • 1
  • 2

2 Answers2

2

you need to stringify the JSON data was sending

$.ajax({
  type: 'POST',
  url: url,
  async: false,
  data: JSON.stringify(jData),
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  success: function(response) {
    console.log(response)

  },
  error: function(jqXHR, textStatus, errorThrown) {


  }
});
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
A.D.
  • 2,352
  • 2
  • 15
  • 25
  • after stringifying the json data, it shows the following errors: – Durga Nov 07 '17 at 06:36
  • 1.Failed to load resource: the server responded with a status of 405 (Method Not Allowed ) – Durga Nov 07 '17 at 06:36
  • 2.Failed to load url :Response for preflight has invalid HTTP status code 405 – Durga Nov 07 '17 at 06:38
  • Maybe something missing in your server configuration tries to configure `Access-Control-Allow-Origin` and `Access-Control-Allow-Origin`.[may this help][1].[may this help][2] [1]: https://stackoverflow.com/questions/36002979/xmlhttprequest-cannot-load-and-response-for-preflight-has-invalid-http-status-co [2]: https://stackoverflow.com/questions/36258959/cors-enabled-but-response-for-preflight-has-invalid-http-status-code-404-when-po – A.D. Nov 07 '17 at 07:07
  • The problem was with the webservice.It was solved.The code is now working. thank you – Durga Nov 09 '17 at 11:25
0

Try removing the open and closing brackets around your jData

var jData = {};

Not

var jData = [{}];