javascript - AngularJS $http.put PUT 方法不发送数据

标签 javascript rest angularjs put http-method

我正在从工作正常的 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/

相关文章:

javascript - 为什么 `this` 指的是全局对象?

javascript - Uncaught ReferenceError : app is not defined in Angularjs

ajax - Laravel 中的 PATCH AJAX 请求

javascript - Angular.js : How do I initialize the ng-model of a <select> element to be the ng-selected option's value?

javascript - 仅使用 Angular 进行路由

javascript - 将 popupWindow = window.open 与 JavaScript 变量一起使用

json - 如何使用 LinkedIn Share API 获取要显示的描述字段

java - 请放心 : Verify xml value for of child tags

javascript - 将第二个服务注入(inject) Controller AngularJS 时遇到错误

angularjs - Angular ui-router 重新加载 ng-include 外部 ui-view