簡體   English   中英

Http Put方法在帶有標題的AngularJS中不起作用

[英]Http Put method not working in AngularJS with headers

我試圖在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服務= 2ms狀態= 404字節= 481

將Content-Type更改為“ Content-Type”:'application / x-www-form-urlencoded; charset = UTF-8'

並且,如果這是跨域資源請求,則需要在服務器中添加設置的CORS標頭(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);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM