AngularJS和ExpressJS:如何阅读由$ http.put()发送的内容

我有angularJS应用程序的问题,它将callback发送到nodejs服务器。 当我使用POST或GET方法,所有工作正常,但是当我发送PUT请求,我得到错误。

如果我从curl调用服务器,它工作正常; 当我使用PUT方法从angularJS调用某个远程服务器时,它也可以正常工作。 所以问题在于我的本地主机上的angularJS和nodejs之间的合作,但是我还没有弄明白。

我的angular度方法,调用本地nodejs服务器:

  $http({ method :'PUT', url:'http://127.0.0.1:3000/s', data: $.param({"test":true, _method: 'PUT'}), headers :{'Content-Type':'application/x-www-form-urlencoded'} }).success(function(data, status, headers, config) { alert('OK'); }).error(function(data, status, headers, config) { alert('error'); }).catch(function(error){ alert('catch' + JSON.stringify(error)); }); 

我的nodejs文件:

 var express = require("express"); var mysql = require('mysql'); var bodyParser = require('body-parser') var methodOverride = require('method-override'); var app = express(); //use body parser to get json app.use(bodyParser.json()) // for parsing application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: true })); // allow PUT from browser app.use(methodOverride('_method')); app.put('/s', function(req, res){ console.log('OK'); //handle_database_put(req, res); res.set('Access-Control-Allow-Origin', '*'); res.set('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); res.send('PUT OK'); }); app.listen(3000); 

编辑这里是Angular应用程序中的错误消息。 服务器从不打印,因为它应该

 {"data":null,"status":0,"config":{"method":"PUT","transformRequest":[null],"transformResponse":[null],"url":"http://127.0.0.1:3000/s","data":"test=true&_method=PUT","headers":{"Content-Type":"application/x-www-form-urlencoded","Accept":"application/json, text/plain, */*"}},"statusText":""} 

编辑2 :我刚刚注意到,该请求不被识别为PUT ,而是作为OPTIONS

尝试这个

 $http({ method :'PUT', url:'http://127.0.0.1:3000/s', params: {"test":true, _method: 'PUT'}, headers :{'Content-Type':'application/json'} }).success(function(data, status, headers, config) { alert('OK'); }).error(function(data, status, headers, config) { alert('error'); }).catch(function(error){ alert('catch' + JSON.stringify(error)); }); 

您的服务器"Accept":"application/json ,你正在发送"Content-Type":"application/x-www-form-urlencoded"所以你必须使用'Content-Type': 'application/json'和变换你的数据到json,像这样:

 function youMethod(){ var myUrl = 'http://127.0.0.1:3000/s'; var data = { test: true, _method: "PUT" }; return $http({ url: myUrl, method: 'PUT', data: JSON.stringify(data), headers: { 'Content-Type': 'application/json' } }).then(youMethodComplete) .catch(youMethodFailed); function youMethodComplete(response) { alert('OK'); console.log(response); } function uyouMethodFailed(response) { alert('ERROR'); console.log(response); } } 

PD:我也推荐 – > 不要使用$ http成功

额外的:如果你想使用"Content-Type":"application/x-www-form-urlencoded" ,你为此configuration你的服务器,并且你想遵循这个语法:

 function youMethod(){ var myUrl = 'http://127.0.0.1:3000/s'; var data = { test: true, _method: "PUT" }; return $http({ url: myUrl, method: 'PUT', data: $httpParamSerializer(data), //remember to inject $httpParamSerializer headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(youMethodComplete) .catch(youMethodFailed); function youMethodComplete(response) { alert('OK'); console.log(response); } function uyouMethodFailed(response) { alert('ERROR'); console.log(response); } } 

我终于find了问题:

会发生什么情况是,浏览器执行预检OPTIONS HTTP请求,并且如果它从服务器接收到状态200,则比继续执行原始PUT调用。

在ExpressJS服务器上,进行了以下configuration,并且来自AngularJS的PUT请求现在使用PUT请求正确发送和接收数据:

 // apply this rule to all requests accessing any URL/URI app.all('*', function(req, res, next) { // add details of what is allowed in HTTP request headers to the response headers res.header('Access-Control-Allow-Origin', req.headers.origin); res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS'); res.header('Access-Control-Allow-Credentials', false); res.header('Access-Control-Max-Age', '86400'); res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'); // the next() function continues execution and will move onto the requested URL/URI next(); }); 

对我来说,仍然是一个谜,为什么POST请求被正确处理,并且PUT请求不是