Firefox 不允许使用 Prototype js 库进行跨域 Ajax GET 请求

发布于 2024-12-17 13:17:58 字数 1937 浏览 0 评论 0原文

我正在尝试执行跨域 ajax 请求并将内容填充到 JSP 页面中的 DIV 中,我使用的 javascript 方法如下,我

function fetchImgLeads(){
        var myAjax =  new Ajax.Request(
                    'http://someotherdomain:8080/imghtml?img=100',
                    {   method:'GET', 
                        parameters:{},
                        requestHeaders :["Access-Control-Allow-Origin","*","Access-Control-Allow-Methods","POST, GET, OPTIONS","Access-Control-Allow-Headers", "X-PINGOTHER","Access-Control-Max-Age","1728000"],
                        onSuccess:function(t){
                            alert(t.responseText.trim());
                            $('imagediv').update(t.responseText);
                        }, 
                        onFailure:function(t){
                            //do something
                        }
                    }
                );  
    }

在加载时调用此方法,并看到一个错误,内容为 HTTP/ 1.1 Firefox Web 控制台中出现 401 Unauthorized。同样的事情在 IE 中也能正常工作。我为此使用 IE 8.0 和 Firefox 8。

除了 requestHeaders 之外,我还需要添加其他内容吗?

捕获的 Http headers 如下,即使 ajax 请求似乎也不起作用,

OPTIONS http://www.google.com/ HTTP/1.1
Host: www.google.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Proxy-Connection: keep-alive
Origin: http://localhost:8080
Access-Control-Request-Method: GET
Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods,access-control-allow-origin,access-control-max-age,x-prototype-version,x-requested-with

HTTP/1.1 405 Method Not Allowed
Content-Type: text/html; charset=UTF-8
Date: Fri, 25 Nov 2011 05:53:54 GMT
Server: GFE/2.0
Content-Length: 11819
Proxy-Connection: Keep-Alive
Connection: Keep-Alive

I am trying to do a cross domain ajax request and populate the contents into a DIV in my JSP page, the javascript method I am using is as follows,

function fetchImgLeads(){
        var myAjax =  new Ajax.Request(
                    'http://someotherdomain:8080/imghtml?img=100',
                    {   method:'GET', 
                        parameters:{},
                        requestHeaders :["Access-Control-Allow-Origin","*","Access-Control-Allow-Methods","POST, GET, OPTIONS","Access-Control-Allow-Headers", "X-PINGOTHER","Access-Control-Max-Age","1728000"],
                        onSuccess:function(t){
                            alert(t.responseText.trim());
                            $('imagediv').update(t.responseText);
                        }, 
                        onFailure:function(t){
                            //do something
                        }
                    }
                );  
    }

I am calling this on load and I see an error that says HTTP/1.1 401 Unauthorized in the Firefox web console. The same thing works fine in IE. I am using IE 8.0 and Firefox 8 for this.

Apart from the requestHeaders, Is there something else I have to add?

The Http Headers captured are as follows, even then the ajax request does not seem to be working,

OPTIONS http://www.google.com/ HTTP/1.1
Host: www.google.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Proxy-Connection: keep-alive
Origin: http://localhost:8080
Access-Control-Request-Method: GET
Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods,access-control-allow-origin,access-control-max-age,x-prototype-version,x-requested-with

HTTP/1.1 405 Method Not Allowed
Content-Type: text/html; charset=UTF-8
Date: Fri, 25 Nov 2011 05:53:54 GMT
Server: GFE/2.0
Content-Length: 11819
Proxy-Connection: Keep-Alive
Connection: Keep-Alive

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残花月 2024-12-24 13:17:58

我面临着同样的问题。

这是我到目前为止发现的:

https://developer.mozilla.org/En/Using_XMLHttpRequest< /a>

(Firefox 3 之前的 Firefox 版本允许您将首选项 Capability.policy..XMLHttpRequest.open 设置为 allAccess 以给予特定站点跨站点访问。不再支持此

  • 操作 。)启用跨站点脚本的推荐方法是在 XMLHttpRequest 的响应中使用 Access-Control-Allow-Origin HTTP 标头。

http://en.wikipedia.org/wiki/XMLHttpRequest#Cross-domain_requests

  • 添加到服务器的 HTTP 响应标头中的标头可以允许跨域请求成功。例如Access-Control-Allow-Origin:*,可以允许所有域访问某个服务器。 Access-Control-Allow-Origin 可用于所有支持跨域请求的浏览器,其中包括 Internet Explorer 8。W3C 的规范在跨域资源共享中定义。

希望这会有所帮助...

i am facing the same issue.

This is what i found out about it so far:

https://developer.mozilla.org/En/Using_XMLHttpRequest

(Versions of Firefox prior to Firefox 3 allowed you to set the preference capability.policy..XMLHttpRequest.open to allAccess to give specific sites cross-site access. This is no longer supported.)

  • The recommended way to enable cross-site scripting is to use the Access-Control-Allow-Origin HTTP header in the response to the XMLHttpRequest.

http://en.wikipedia.org/wiki/XMLHttpRequest#Cross-domain_requests

  • Headers added to a server's HTTP response headers can allow cross-domain requests to succeed. For example, Access-Control-Allow-Origin: *, can allow all domains to access a server. Access-Control-Allow-Origin can be used in all browsers that support cross-domain requests, which includes Internet Explorer 8. The W3C's specification is defined in Cross-Origin Resource Sharing.

Hope this will help...

帥小哥 2024-12-24 13:17:58

您正在尝试通过请求发送“Access-Control-Allow-*”标头。

相反,您的服务器应该回复这些标头。

CORS(预检)的工作方式如下:

  • 浏览器向服务器请求发送请求的权限:Access-Control-Request-*标头(当您尝试执行跨域请求时,浏览器会自动添加它们)< /p>

  • 服务器响应Access-Control-Allow-* 标头使浏览器知道是否允许发送真实请求

Curl 命令应该向您显示类似的内容:

curl -v -H 'Origin: http://myserver' -X OPTIONS -H 'Access-Control-Request-Methods: GET' -H 'Access-Control-Request-Headers: X-Requested-With' http://someotherdomain:8080/imghtml?img=100
* Connected to someotherdomain port 8080 (#0)
> OPTIONS /imghtml?img=100 HTTP/1.1
> User-Agent: curl/7.30.0
> Host: someotherdomain:8080
> Accept: */*
> Origin: http://myserver
> Access-Control-Request-Methods: GET
> Access-Control-Request-Headers: X-Requested-With
> 
< HTTP/1.1 200 OK
< Date: Wed, 08 May 2013 14:34:45 GMT
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: X-Requested-With
< Access-Control-Allow-Methods: GET
< Access-Control-Max-Age: 86400
< Content-Length: 0
< Content-Type: text/plain
< 
* Connection #0 to host someotherdomain left intact

如果您对向服务器发送任何自定义标头不感兴趣。然后只需删除 Access-Control-Allow-Headers:

You are trying to send 'Access-Control-Allow-*' headers with request.

Instead you server should reply with these headers.

CORS (preflight) works this way:

  • Browser asks from server permission to send request: Access-Control-Request-* headers (Browser adds them automatically when you try to do cross domain request)

  • Server responds with Access-Control-Allow-* headers making browser know if it is allowed to send real request

Curl command should show you something like that:

curl -v -H 'Origin: http://myserver' -X OPTIONS -H 'Access-Control-Request-Methods: GET' -H 'Access-Control-Request-Headers: X-Requested-With' http://someotherdomain:8080/imghtml?img=100
* Connected to someotherdomain port 8080 (#0)
> OPTIONS /imghtml?img=100 HTTP/1.1
> User-Agent: curl/7.30.0
> Host: someotherdomain:8080
> Accept: */*
> Origin: http://myserver
> Access-Control-Request-Methods: GET
> Access-Control-Request-Headers: X-Requested-With
> 
< HTTP/1.1 200 OK
< Date: Wed, 08 May 2013 14:34:45 GMT
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: X-Requested-With
< Access-Control-Allow-Methods: GET
< Access-Control-Max-Age: 86400
< Content-Length: 0
< Content-Type: text/plain
< 
* Connection #0 to host someotherdomain left intact

If you are not interested in sending any custom headers to server. Then just drop Access-Control-Allow-Headers: line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文