我将数据从我的视图发送给控制器。
$.ajax({
type: "POST",
url: "/registerAgency",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
success: function(responseData, textStatus, jqXHR) {
alert("data saved")
},
error: function(jqXHR, textStatus, errorThrown) {
alert("error");
}
})
});我不明白为什么我的请求不使用内容类型application/json,而是使用application/x-www-form-urlencoded
这个论坛上有这样一个问题,但我真的不知道如何解决这个问题。
发布于 2016-02-06 18:50:10
我对这个问题也有一点好奇,因为我通常不使用contentType: 'application/json'。它的处理方式与使用$_POST、$_GET或$_REQUEST有很大不同。当然,它是json内容类型。
jquery
var data = { "agencyName":"sadf", "description":"asdf", "phoneNumber":"1111111111121", "webSite":"", "address":{ "country":"asdfasdf", "region":"asdf", "postalCode":"23423", "locality":"asdfasdf", "additionalInfo":"asdfasd" } };
$.ajax({
type: "POST",
url: ".../registerAgency.php",
data: JSON.stringify( data ),
contentType: 'application/json; charset=utf-8',
success: function(responseData, textStatus, jqXHR) {
alert(responseData);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("error");
}
});php/server
<?php
// parameters are not accessible via POST, GET, nor REQUEST
$res = json_decode( file_get_contents('php://input') );
var_dump($res);
?>参考资料:Ajax call with contentType: 'application/json' not working
https://stackoverflow.com/questions/35244543
复制相似问题