无法在 $.ajax 请求上设置 HTTP Host 标头 [英] Not able to set HTTP Host header on $.ajax request

查看:140
本文介绍了无法在 $.ajax 请求上设置 HTTP Host 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个话题之前已经以不同的方式讨论过,但我还没有找到适合我的解决方案.我使用的是 jquery 1.7.1.

This topic has been discussed in different flavors before, but I have not yet found a solution that works for me. I'm using jquery 1.7.1.

我有一个可以返回 JSON 和 XML 的 REST 风格的 Web 服务,但是对于这个项目,我需要使用 XML 端点.为了绕过跨域限制,我使用 Perl 的 HTTP::Proxy 创建了一个透明代理.这个代理轻而易举,因为我可以从任何地方与 HTTP GET 客户端进行通信.

I have a REST style web service that can return both JSON and XML, but for this project I need to use the XML endpoint. To get around the cross domain restrictions, I've created a transparent proxy using Perl's HTTP::Proxy. This proxy works as a breeze since I can talk to it with HTTP GET clients from anywhere.

由于它是一个透明代理,它依赖于客户端请求中设置的 Host 标头.我的代码如下所示:

Since it is a transparent proxy, it relies on the Host header being set in the client request. My code looks like this:

  $.ajax({
    type: "GET",
    crossDomain: true, // not needed I think
    error: function() { alert('Failed ..'); },
    url: "http://www.skiforeningen.no:8080/<remote REST URL>",
    dataType: "xml",
    headers: {'Host': 'remote REST host'},
    success: parseXml,
});

Chrome 非常明确,并说

Chrome is very explicit, and says

拒绝设置不安全的标头Host"

Refused to set unsafe header "Host"

XMLHttpRequest 无法加载 http://www.skiforeningen.no:8080/sted/Norge/Oslo/Oslo/Skansebakken/varsel.xml.Access-Control-Allow-Origin 不允许使用 http://www.skiforeningen.no

XMLHttpRequest cannot load http://www.skiforeningen.no:8080/sted/Norge/Oslo/Oslo/Skansebakken/varsel.xml. Origin http://www.skiforeningen.no is not allowed by Access-Control-Allow-Origin

但是代理和带有 jquery 的 HTML 页面都位于主机 www.skiforeningen.no 上.

but both the proxy and the HTML page with jquery sits on the host www.skiforeningen.no.

FF 也拒绝设置 Host 标头(显然),因为触发了错误事件处理程序.

FF also refuses to set the Host header (apparently) since the error event handler is triggered.

谢谢,

推荐答案

您不能在不同的主机上执行 XHR 请求,就像错误消息告诉您的那样.不同的端口意味着不同的主机.

You cannot do an XHR request on a different host, juste like the error message tells you. A different port means a different host.

参见:我可以在与从该端口加载的脚本文件不同的端口上使用 XMLHttpRequest 吗?

这篇关于无法在 $.ajax 请求上设置 HTTP Host 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆