我想正确配置grunt-connect-proxy进行开发,避免出现CORS问题。
我有
我安装了npm模块:
>npm install grunt-connect-proxy --save-dev
grunt-connect-proxy@0.2.0 node_modules\grunt-connect-proxy
├── lodash@0.9.2
└── http-proxy@1.11.3 (requires-port@0.0.1, eventemitter3@1.2.0)
我的Gruntfile.js配置
require('jit-grunt')(grunt, {
(...)
configureProxies: "grunt-connect-proxy"
});
(...)
connect: {
options: {
port: 9000,
hostname: 'localhost',
livereload: 35729
},
proxies: [
{
context: ['/mywebservice'],
host: 'localhost',
port: 8080,
changeOrigin: true
}
],
livereload: {
options: {
open: true,
middleware: function (connect) {
return [
require('grunt-connect-proxy/lib/utils').proxyRequest,
connect.static('.tmp'),
(...)
connect.static(appConfig.app)
];
}
}
},
(...)
}
grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
(...)
grunt.task.run([
(...)
'configureProxies:server',
(...)
]);
});
查询听起来没有通过代理,因为它仍然可以看到原始http://localhost:9000。我收到以下错误:
XMLHttpRequest cannot load http://localhost:8080/xmlcompare-rs/xmlcompare. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
虽然grunt serve命令打印
Running "configureProxies:server" (configureProxies) task
Proxy created for: /mywebservice to localhost:8080
响铃吗?
答案 0 :(得分:1)
问题似乎来自您的REST服务服务器配置。 使用CORS过滤器来避免此异常。
https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter
实现可能因您使用的堆栈而异。
我希望它有所帮助