我正在从工作正常的 jquery $.ajax 切换到使用 AngularJS $http.put 来访问 Restful API。
我可以进行 API 调用,但 PUT 数据未发送 - 因此我的 API 看到带有空数据对象的 PUT 请求,该对象应包含 JSON 字符串 -> data.values = 'a json结构'
$http.put(
$rootScope.api_url,
{
values: jsonifiedValues
},
{
headers: {
apihash: sha256hash
}
}).success(function(data,status,headers,config){
// handle success
}).error(function(data,status,headers,config) {
// handle failure
});
我以前没有使用过 AngularJS 的 $http,但是当我转储 PHP api 中的数据时,它只是空的。这就是我从 PHP 请求中提取它的方法:
parse_str(file_get_contents('php://input'), $put_vars);
$arr_req_data = $put_vars['values'];
在我的 API 中,如果从请求发送的 apihash 与基于 PUT 值构建的 sha256 哈希不匹配,则会失败。
这在 JQuery 中工作,只是失败了,现在我已经切换到 $http。我不知道为什么 PUT 数据似乎是空的。
最佳答案
file_get_contents('php://input')
的返回值将是一个 JSON 字符串(假设所有内容都已发送),因此 parse_str
不是处理该数据的正确函数。
而是使用 json_decode
.
此外,也无需发送 json 化值,这只会使事情变得更加复杂,因为您必须使用 json_decode
两次。
关于javascript - AngularJS $http.put PUT 方法不发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14896169/