javascript - Http Put 方法在带有 header 的 AngularJS 中不起作用

标签 javascript angularjs http ionic-framework

我尝试在 http angularJS 中使用 put 方法,但出现错误

错误:XMLHttpRequest 无法加载 [url]。预检响应具有无效的 HTTP 状态代码 404

我的代码是:

var newRentalUrl = serverURL;
var dataObj = JSON.stringify(JSONDATA);

$http.put(newRentalUrl, dataObj, {
  headers: {
    'Content-Type': 'application/json; charset=UTF-8'
  },
}).success(function(responseData) {
  try {
    $ionicLoading.hide();
    console.log(responseData);
    //$state.go('payment');
  } catch (err) {
    console.log(err);
    $ionicLoading.hide();
  }
  $ionicLoading.hide();
}).error(function(data, status, headers, config) {
  $ionicLoading.hide();
  console.log(data);
});

在服务器端,该方法作为选项被接收

2015-10-21T05:37:02.947517+00:00 heroku[router]: at=info method=OPTIONS path="/v1/home/xx" host=xxxxxx.herokuapp.com request_id=xxxxx-498d-4026-a39a-xxxxxxx fwd="xxx.xxxx.xxx.xxx" dyno=web.1 connect=0ms service=2ms status=404 bytes=481

最佳答案

将 Content-Type 更改为 'Content-Type': 'application/x-www-form-urlencoded;字符集=UTF-8'

如果是跨域资源请求,您需要在服务器中添加设置 CORS header ( Access-Control-Allow-Origin )。

希望它能起作用。

  var newRentalUrl = serverURL;
        var dataObj = JSON.stringify(JSONDATA);

    $http.put(newRentalUrl, dataObj, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      },
    }).success(function(responseData) {
      try {
        $ionicLoading.hide();
        console.log(responseData);
        //$state.go('payment');
      } catch (err) {
        console.log(err);
        $ionicLoading.hide();
      }

  $ionicLoading.hide();
}).error(function(data, status, headers, config) {
  $ionicLoading.hide();
  console.log(data);
});

关于javascript - Http Put 方法在带有 header 的 AngularJS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251837/

相关文章:

javascript - Jquery UI 可拖动在屏幕底部创建一个空白

javascript - jquery:从ajax调用获取父元素

javascript - Browser.wait() 直到 .getAttribute() 返回 true

javascript - 是否可以在 angularjs 中对 ui.router 的 resolve 执行 $http get 请求?

asp.net - 如何将敏感数据发送到 Asp.net Web API 服务器

http - 使用 Content-Type 对 HTTP GET 的正确响应

javascript - 无法在 beforeunload 事件上重置 Widow

javascript - 在 AngularJs 中调用函数 onclick

javascript - 为什么在 Angular 中,当我们不分配函数时,作用域属性的变化不会动态地反射(reflect)回 View ?

java - RESTEasy 是否有一个好的 BrowserCache 实现? (比 LightweightBrowserCache 更好)