【问题标题】:AngularJS $http.put PUT Method not sending DataAngularJS $http.put PUT 方法不发送数据
【发布时间】:2013-02-15 13:48:13
【问题描述】:

我正在从运行良好的 jquery $.ajax 切换到使用 AngularJS $http.put 来访问 restful API。

我可以进行 API 调用,但未发送 PUT 数据 - 所以我的 API 看到一个带有空数据对象的 PUT 请求,它应该包含一个 JSON 字符串 -> data.values = 'a json structure'

$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 数据似乎是空的。

【问题讨论】:

  • 您是否使用浏览器的开发工具查看了浏览器实际发送的内容?
  • 好点,我没有检查,但数据正在发送中

标签: javascript rest angularjs put http-method


【解决方案1】:

file_get_contents('php://input') 的返回值将是 JSON 字符串(假设所有内容都已发送),因此 parse_str 不是处理该数据的正确函数。
请改用json_decode

也没有必要发送 jsonified 值,它只会让事情变得更复杂,因为你必须使用 json_decode 两次。

【讨论】:

  • 成功了,谢谢。不知道为什么 AngularJS 不工作时 jQuery 工作。 AJAX 方法看起来很相似
  • 有什么办法让$http.put不发送json对象,而是发送正确格式的对象?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 2015-11-27
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多