Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  76] [ 7]  / answers: 1 / hits: 37347  / 11 Years ago, wed, october 23, 2013, 12:00:00

I have the following jQuery AJAX request:



// collect form data and create user obj
var user = new User();
user.firstname = $(#usrFirstName).val();
user.lastname = $(#usrSurname).val();
user.role = $(#usrRole).val();

// actual ajax request
$.ajax({
type: 'POST',
url : 'http://awesome-url',
crossDomain: true,
data: user,
contentType:application/json; charset=utf-8,
dataType: 'json'
}).done(function(data, status) {
alert(JSON.stringify(data));
}).fail(function(data, status) {
alert(status);
alert(JSON.stringify(data));
});


The response from the Server is:




status:400,statusText:Bad Request

The request sent by the client was syntactically incorrect.




The server is running Spring-MVC. But as far as I can tell it is working correctly. Because if I'm sending a request manually with Postman and the following configuration it works.



Header:



Content-Type application/json; charset=utf-8


Content:



{firstname:alex,lastname:lala,role:admin}


I have to mention that it is a cross-domain request (for the time developing, it will be hosted on the same domain as the server later). I did disable the security settings in the browser and AJAX requests to the server are working fine (as long as I don't have to send data).


More From » jquery

 Answers
12

you need to serialize your json, try:



$.ajax({
type: 'POST',
url : 'http://awesome-url',
crossDomain: true,
data: JSON.stringify(user),
contentType:'application/json; charset=utf-8',
dataType: 'json'
})

[#74783] Tuesday, October 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sabinal

Total Points: 144
Total Questions: 112
Total Answers: 107

Location: Ghana
Member since Mon, Aug 22, 2022
2 Years ago
;