我是这样提交表单的
$http({
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
url: url,
data: {
type: 1
}
})
为什么我的服务器接到的会是这样的东西?
{ '{"type":1}': '' }
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
问题解决了
原因:
By default, jQuery transmits data using Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe serialization. AngularJS, however, transmits data using Content-Type: application/json and { "foo": "bar", "baz": "moe" } JSON serialization
默认情况下,jQuery传输数据使用Content-Type: x-www-form-urlencodedand和类似于"foo=bar&baz=moe"的序列,然而AngularJS,传输数据使用Content-Type: application/json和{ "foo": "bar", "baz": "moe" }这样的json序列。
解决方法
可能是你jquery用习惯了,
jquery封装了query_string操作
你在angular加上这段代码就好了,改下示例名称
1、看angularjs的官方文档了解其用法,貌似$http的header有点不同。
2、下载fiddler工具,查看请求header和params等信息。
服务端不能直接用 $_REQUEST/$_POST获取到,而需要用:
$params = json_decode(file_get_contents('php://input'),true);
来获取提交数据