2015-10-21 86 views
0

我试图使用put方法在HTTP angularJS我得到一个错误HTTP PUT方法AngularJS不工作与头

错误: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

+0

您是否研究过错误以了解其含义? – charlietfl

+0

是的,我做了,这个错误是put方法没有赶上服务器。 – kapilkarda

回答

1

将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); 
}); 
+0

您好,感谢您的回复,我曾尝试过但没有收到结果。 – kapilkarda