Ric*_*lva 6 ajax proxy jquery ember.js ember-cli
我是一个初学者,使用ember-cli v0.0.47并努力让http-proxy工作.
我正在尝试向远程OGC CSW服务器发出ajax请求.该请求是具有一些附加参数的普通HTTP GET请求,并且预期响应是XML文档.
由于我正在做一个跨源请求,我决定使用服务器代理,以避免处理CORS的东西.
我使用ember-cli生成代理配置:
ember-cli generate http-proxy geoland2 http://geoland2.meteo.pt
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我定义了一个使用jquery.ajax与服务器通信的"搜索"操作:
export default Ember.Controller.extend({
actions: {
search: function() {
Ember.$.ajax({
url: 'geoland2/geonetwork/srv/eng/csw',
contentType: 'application/xml',
crossDomain: true,
xhrFields: {
withCredentials: true
},
dataType: 'xml',
data: {
service: 'CSW',
version: '2.0.2',
request: 'GetCapabilities'
},
}).then(
function(data) {
alert(data);
Ember.$('.results').html(data);
},
function(jqXHR, textStatus, errorThrown) {
Ember.$('.results').html(jqXHR.status + ' ' + errorThrown + ' - ' + jqXHR.responseText);
}
);
}
}
});
Run Code Online (Sandbox Code Playgroud)
现在当这个动作被触发时,我会期待这个动作
geoland2/geonetwork/srv/eng/csw
Run Code Online (Sandbox Code Playgroud)
将由ember-cli的服务器代理并发送给
http://geoland2.meteo.pt/geonetwork/srv/eng/csw?service=CSW&version=2.0.2&request=GetCapabilitites
Run Code Online (Sandbox Code Playgroud)
这种假设应该是正确的吗?
实际上,我看到的是请求根本没有代理.ember应用程序尝试联系
http://localhost:4200/geoland2/geonetwork/srv/eng/csw?service=CSW&version=2.0.2&request=GetCapabilitites
Run Code Online (Sandbox Code Playgroud)
它失败并出现404 HTTP错误,因为指定的资源显然不可用.
我已经server/proxies/geoland2.js通过proxyPath使用URL的其余部分注释加入变量的行来编辑自动生成的文件:
var proxyPath = '/geoland2';
module.exports = function(app) {
// For options, see:
// https://github.com/nodejitsu/node-http-proxy
var proxy = require('http-proxy').createProxyServer({});
var path = require('path');
app.use(proxyPath, function(req, res, next){
// include root path in proxied request
//req.url = path.join(proxyPath, req.url); // I commented this line
proxy.web(req, res, {target: 'http://geoland2.meteo.pt:80'});
});
};
Run Code Online (Sandbox Code Playgroud)
这似乎适合我的用例,因为我的服务器端点在
http://geoland2.meteo.pt/geonetwork/srv/eng/csw
Run Code Online (Sandbox Code Playgroud)
并不是
http://geoland2.meteo.pt/geoland2/geonetwork/srv/eng/csw
Run Code Online (Sandbox Code Playgroud)
我相信即使这种改变可能是错误的,我也应该从原始服务器上取回一些东西.
我是否还需要修复一些与CORS相关的问题以使代理工作?或者可能还有一些文件需要编辑才能正确设置http-proxy?
在当前版本的 ember cli 中,您可以使用指向外部服务器的代理选项来启动您的应用程序。
ember server --proxy http://externalserver.ccc/api/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2327 次 |
| 最近记录: |