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

查看:172
本文介绍了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");

可以正常工作。我也试图分配$ .ajax到一个变量像

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

推荐答案

如果你使用aws s3(我认为这是适用的,否则),问题可能是一个缺失的CORS配置标签。我碰到了一个类似的缺失的问题。这是我完成的配置:

If you're using aws s3 (and I assume this is applicable otherwise), the problem is possibly a missing CORS configuration tag. I ran in to a similar problem with missing. Here's my completed configuration:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://localhost:3000</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
        <ExposeHeader>*</ExposeHeader>
    </CORSRule>
</CORSConfiguration>

AllowedHeader设置访问控制请求标头标头和ExposeHeader设置 Access-Control-Expose-Headers 标头,浏览器不允许javascript使用返回的标头。

AllowedHeader sets the Access-Control-Request-Headers header, and ExposeHeader sets Access-Control-Expose-Headers header, without which the browser wont allow javascript to use the returned headers.

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

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