简体   繁体   中英

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) ".

在此处输入图片说明

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

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


  }
});

Try removing the open and closing brackets around your jData

var jData = {};

Not

var jData = [{}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM