Durga
Durga

Reputation: 11

showing 400 (Bad Request) in ajax

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

Upvotes: 1

Views: 7085

Answers (2)

Clarence Troy
Clarence Troy

Reputation: 1

Try removing the open and closing brackets around your jData

var jData = {};

Not

var jData = [{}];

Upvotes: 0

A.D.
A.D.

Reputation: 2372

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) {


  }
});

Upvotes: 2

Related Questions