jQuery的跨域请求响应头 [英] jQuery cross domain request response headers

查看:383
本文介绍了jQuery的跨域请求响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与HTML数据类型的跨域Ajax请求。 他们的工作好,因为我有

I am making cross domain ajax requests with html data type. They work OK as I include

Access-Control-Allow-Origin

在来自服务器的响应。问题是,我需要得到一定的头从服务器的响应,无论我做什么,响应头除了内容类型返回null。

in the response from the server. Problem is I need to get certain headers from the server's response and whatever I do, response headers apart from "content type" return null.

jQuery不会请求,检索包括标题(我可以从交通看出来)的反应,但它不解析它。

jQuery does the request, retrieves the response including headers (I can see it from the traffic) but it doesn't parse it.

我曾试过用

crossDomain: true

这并没有帮助。这里是来自服务器的样品反应。

It didn't help. Here is the sample response from the server.

Access-Control-Allow-Origin:*
Cache-Control:private
Content-Encoding:gzip
Content-Length:514
Content-Type:text/html; charset=utf-8 
X-MYRESPONSEHEADER:1

如果请求和响应文件的同一台服务器上

If requesting and responding document are on same server

 success: function (data, status, xhr) {
        totalRows = xhr.getResponseHeader("X-MYRESPONSEHEADER");

工作正常。我也曾尝试分配$阿贾克斯给一个变量如

works fine. I have also tried to assign $.ajax to a variable like

var jQxhr = $.ajax(.....

我不明白为什么它不会被解析,因为jQuery的实际发出请求并得到响应

I don't see why it wouldn't be parsed since jQuery actually makes the request and gets the response

任何想法?我失去了一些东西?

Any ideas? Am I missing something?

发送到请求头

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-MYRESPONSEHEADER
Access-Control-Allow-Methods: POST
Access-Control-Allow-Methods: GET
X-MYRESPONSEHEADER: 24
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 29 Feb 2012 11:34:21 GMT

的Content-Length:514

Content-Length: 514

推荐答案

您需要添加另一个CORS的头在服务器响应,访问控制 - 允许 - 头。在这种情况下,

You need to add another CORS-specific header in the server response, Access-Control-Allow-Headers. In this case,

Access-Control-Allow-Headers: X-MYRESPONSEHEADER

编号:的https://developer.mozilla.org/en/http_access_control#Access-Control-Allow-Headers

这篇关于jQuery的跨域请求响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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