issue: to ensure that the background program is not a problem, through the postman can successfully submit the form, but from the front page with Ajax submitted but always execute the error callback function, debug, found that postman sent the request in the format of the data in JSON, But the debug front page sends a string of Attribute1=value1&attribute2=value2&. The problem is where.
Background interface:
/*controller request url*/
var url = "${ctx}/developer/register";
/* Contract Data Format */
Consume=mediatype.application_json_value
JSON data:
Json_data = {
"loginName": loginName,
"password": Password1,
"name": Name,
"email": Email,
" Sex ": Sex,
" Avatar ": Avatar,
" description ": Description
}
Ajax Request Code-mode one:
<span style= "line-height:1.5;" >$.ajax ({
type: "POST",
Url:url,
contentType: "Application/json",
dataType: "JSON",
data: Json_data,
success:function () {
Window.alert ("Add developer succeeded");
},
error:function (XMLHttpRequest) {
Alert ("Request error:" +xmlhttprequest.status+xmlhttprequest.statustext);
}
}); </span>
Ajax request Result: Each time is execution error, error code: 400,bad request.
Ajax Request Code-mode two:
$.ajax ({
type: "POST",
Url:url,
contentType: "Application/json",
dataType: "JSON",
data: Json_data,
success:function () {
Window.alert ("Add developer succeeded");
},
error:function (XMLHttpRequest) {
Alert ("Request error:" +xmlhttprequest.status+xmlhttprequest.statustext);
}
});
Ajax Request Results: Pop-up "Add Developer Success" dialog box, solve the problem.
Reason: Controller and foreground conventions deliver JSON-formatted data, so the foreground Ajax must pass the JSON-formatted data, and by looking at the documentation it is known that the Data property receives a string of parameter types, Therefore, you use Json.stringify to convert the JSON object to a string type.
Attached >>jquery Ajax method Link: http://www.w3cschool.cn/ajax_ajax.html