AngularJS和ExpressJS:如何读取$ http.put()发送的内容 [英] AngularJS and ExpressJS: how to read content sent by $http.put()
问题描述
我对angularJS应用程序有问题,该应用程序将回调发送到nodejs服务器.当我使用POST或GET方法时,所有方法都可以正常工作,但是,当我发送PUT请求时,会出现错误.
I have problem with angularJS application, which sends callbacks to nodejs server. When I use POST or GET methods, all works fine, however when I send PUT request, I get error.
如果我从curl
呼叫服务器,则工作正常;当我使用PUT方法从angularJS调用某些远程服务器时,它也可以正常工作.因此,问题出在本地主机上的angularJS和nodejs之间的合作中,但是我还无法弄清楚.
If I call the server from curl
, It works fine; when I call some remote server from angularJS using PUT method, it also works all right. So the problem lies in cooperation between angularJS and nodejs on my localhost, but I haven't been able to figure it out yet.
我的角度方法,调用本地nodejs服务器:
My angular method, calling local nodejs server:
$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文件:
My nodejs file:
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应用中的错误消息.服务器从不按需打印OK
EDIT Here is the error message in Angular app. The server never print OK
as it should
{"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
EDIT 2: I have just noticed, that the request is not recognized as PUT
, but as OPTIONS
推荐答案
我终于找到了问题:
发生的情况是,浏览器发出了预检OPTIONS
HTTP请求,并且如果它从服务器接收到状态200,则继续进行原始PUT调用.
What happens is, that the browser makes a preflight OPTIONS
HTTP request, and if it receives status 200 from server, than in proceeds to the original PUT call.
在ExpressJS服务器上,进行了以下配置,并且来自AngularJS的PUT请求现在可以正确地使用PUT请求发送和接收数据:
On the ExpressJS server, following configuration were made and the PUT request from AngularJS is now correctly sending and receiving data using PUT request:
// 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
请求
It is still a mystery to me, why POST
request were correctly processed and PUT
requests were not
这篇关于AngularJS和ExpressJS:如何读取$ http.put()发送的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!