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

4
  • "the server responded with a status of 400 (Bad Request)" -> Check the request and what the server expects Commented Nov 7, 2017 at 6:15
  • 400 error means some parameters are missing. Commented Nov 7, 2017 at 6:16
  • then something is wrong with your jData json or api might expect different contenttype Commented Nov 7, 2017 at 6:17
  • but when I run the same url in the postman ,the right answer is getting Commented Nov 7, 2017 at 6:42

2 Answers 2

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) { } }); 
Sign up to request clarification or add additional context in comments.

5 Comments

after stringifying the json data, it shows the following errors:
1.Failed to load resource: the server responded with a status of 405 (Method Not Allowed )
2.Failed to load url :Response for preflight has invalid HTTP status code 405
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]: stackoverflow.com/questions/36002979/… [2]: stackoverflow.com/questions/36258959/…
The problem was with the webservice.It was solved.The code is now working. thank you
0

Try removing the open and closing brackets around your jData

var jData = {};

Not

var jData = [{}];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.