【问题标题】:AngularJS $resource makes HTTP OPTIONS request instead of HTTP POST for $save methodAngularJS $resource 为 $save 方法发出 HTTP OPTIONS 请求而不是 HTTP POST
【发布时间】:2014-02-08 21:30:17
【问题描述】:

我正在编写一个简单的库应用程序,以便为使用 AngularJS 的更大项目做好准备。在网上阅读了很多关于使用 $resource 与 RESTful API 交互的内容后,我认为它可能会提供一些节省时间和扩展优势来实现它,而不是为每个请求使用 $http。问题是,由于某种原因(我不是 CORS 方面的专家,并且请求正在跨域发送),当使用我的 Node.js 控制台显示的 $save 方法时:

OPTIONS /books 200 1ms - 161b 

使用query() 方法可以正常工作 - 节点控制台显示:

GET /books 200 1ms - 228b

此时我已经被困了几个小时,尝试了下面的变体,但它总是最终成为$save 的 OPTIONS 请求而不是 POST(根据 Angular 文档应该是这样)方法。

AngularJS 网络应用

app.js

var libraryApp = angular.module('libraryApp', ['ngResource', 'ngRoute', 'libraryControllers']);

libraryApp.factory('$book', ['$resource', function ($resource) {

    return $resource('http://mywebserver\\:1337/books/:bookId', { bookId: '@bookId' });
}]);

controllers.js

var libraryControllers = angular.module('libraryControllers', []);

libraryControllers.controller('BookCtrl', ['$scope', '$book', function($scope, $book) {

    ...

    $scope.addBook = function () {
        var b = new $book;
        b.isbn = "TEST";
        b.description = "TEST";
        b.price = 9.99;
        b.$save();
    };
}]);

带有 Express REST API 的 Node.js

app.js

var express = require('express'),
    books = require('./routes/books'),
    http = require('http'),
    path = require('path');

var app = express();

...

// enable cross-domain scripting
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", req.headers.origin);
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    next();
});

// routing
app.get('/books', books.getAll);
app.get('/books/:isbn', books.get);
// This is what I want to fire with the $save method
app.post('/books', books.add);

http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
});

./routes/books.js

...

exports.add = function(req, res) {

    console.log("POST request received...");
    console.log(req.body.isbn);
};

尝试将此行放入我的配置函数delete $httpProvider.defaults.headers.common["X-Requested-With"];,但没有改变。

我不是 Angular/Node 专业人士,但现在我认为这与它是跨域有关,就像我说的,我不是 CORS 方面的专家。

提前致谢。

【问题讨论】:

标签: node.js angularjs express cors ngresource


【解决方案1】:

我知道回答我自己的问题可能不好,但我在发布此问题几天后发现了问题。

这一切都取决于浏览器如何管理 CORS。当在 JavaScript 中发出不“简单”的跨域请求时(即 GET 请求 - 这解释了 query() 函数起作用的原因),浏览器将自动向指定的 URL/URI 发出 HTTP OPTIONS 请求,称为“飞行前”请求或“承诺”。只要远程源返回 200 的 HTTP 状态代码以及有关它将在响应标头中接受的内容的相关详细信息,那么浏览器就会继续执行原始 JavaScript 调用。

这是一个简短的 jQuery 示例:

function makeRequest() {
    // browser makes HTTP OPTIONS request to www.myotherwebsite.com/api/test
    // and if it receives a HTTP status code of 200 and relevant details about
    // what it will accept in HTTP headers, then it will make this POST request...
    $.post( "www.myotherwebsite.com/api/test", function(data) {
        alert(data);
    });
    // ...if not then it won't - it's that simple.
}

我所要做的就是在响应标头中添加服务器将接受的详细信息:

// 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();
});

然后在 Express 路由之前插入这几行,以简单地为每个 OPTIONS 请求返回一个 HTTP 200 状态代码:

// fulfils pre-flight/promise request
app.options('*', function(req, res) {
    res.send(200);
});

希望这将帮助任何在此页面上遇到同样问题的人。

【讨论】:

  • 回答自己的问题一点也不差,事实上,如果您自己找到问题的解决方案,这是值得鼓励的。
  • 我担心额外的 http 'OPTONS' 请求。有没有办法避免来自 angularjs 的 OPTION 请求?
  • 不幸的是,浏览器在 CORS 请求中没有这样做。
  • 如果您不允许更改服务器实现,一个可能的解决方法是使用带有选项 --disable-web-security 的 chromium-browser。我在 Ubuntu 中从任何位置对其进行了测试:$ chromium-browser --disable-web-security &
【解决方案2】:

我实际上并没有尝试过,但是告诉资源如何处理 $save 请求还不够吗?

$resource('http://mywebserver\\:1337/books/:bookId', { bookId: '@bookId' }, {save: {method: 'POST'});

【讨论】:

  • 不,问题是因为它是一个跨域请求。即使我使用了您提供的方法,浏览器仍然会发出 OPTIONS 请求。
  • 对,只是出于好奇创建了一个plunker。同样在这里,POST 被转换为 OPTIONS。
猜你喜欢
  • 2019-11-30
  • 2014-04-16
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
  • 2019-09-12
  • 2015-08-05
相关资源
最近更新 更多