我正在开发一个应用程序,其他人负责开发API。它完全存在于不同的机器上,但是当我尝试ping它时,我得到了CORS错误。我需要设置http-proxy
以便我可以绕过它。为此,我使用grunt-connect-proxy
。
问题是我的请求没有被捕获,因此他们没有访问API。
connect : {
options : {
port : 9000,
// Change this to '0.0.0.0' to access the server from outside.
hostname : '0.0.0.0',
// hostname : 'localhost',
livereload : 35729,
useAvailablePort : true,
},
livereload : {
options: {
open : true,
base : [
'.tmp',
'<%= yeoman.app %>'
],
middleware: function (connect, options) {
if (!Array.isArray(options.base)) {
options.base = [options.base];
}
// Setup the proxy
var middlewares = [require('grunt-connect-proxy/lib/utils').proxyRequest];
// Serve static files.
options.base.forEach(function(base) {
middlewares.push(connect.static(base));
});
// Make directory browse-able.
var directory = options.directory || options.base[options.base.length - 1];
middlewares.push(connect.directory(directory));
return middlewares;
}
},
},
test : {
options: {
port : 9021,
base : [
'.tmp',
'test',
'<%= yeoman.app %>'
]
}
},
dist : {
options : {
base : '<%= yeoman.dist %>'
}
},
proxies : [{
context : '/api',
host : '10.0.4.121',
port : 8443,
https : true,
changeOrigin : false,
rewrite : {
'/api' : '/evidence/api/v2/ui/'
}
}]
},
当我运行grunt serve
并尝试一些AJAX调用时,他们仍然会http://localhost:9000/api/etc
。这是Fiddler的原始标题:
GET http://localhost:9000/api/ping/ HTTP/1.1
Host: localhost:9000
Connection: keep-alive
Accept: application/json, text/plain, */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Referer: http://localhost:9000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6