1
在以下代碼中,將ContentType設置爲application/json不起作用(服務器發送Bad請求),但將其設置爲text/json。爲什麼?application/json不起作用,但是text/json不起作用
/*converts form Array to JSON*/
function objectifyForm(formArray) {//serialize data function
returnArray = {};
for (var i = 0; i < formArray.length; i++){
returnArray[formArray[i]['name']] = formArray[i]['value'];
}
return returnArray;
}
$(document).ready(function(){
// click on form submit
$('#registration-form').on('submit',function(e){
e.preventDefault();
var details = JSON.stringify(objectifyForm($(this).serializeArray()));
console.log(details)
// send ajax
$.ajax({
url: '/newreg', // url where to submit the request
type : "POST", // type of action POST || GET
datatype : "json", // data type
/* this doesn't work*/
//contentType: "application/json; charset=utf-8",
/*this does*/
contentType: "text/json; charset=utf-8",
data : details,
/*handle success/failure*/
success : function(result) {},
error: function(xhr, resp, text) {}
})
});
});