js 获取ajax对象属性值,ajax对象属性withCredentials

默认情况下,ajax跨源请求不提供凭据(cookie、HTTP认证及客户端SSL证明等)。通过将设置ajax的withCredentials属性设置为true,可以指定某个请求应该发送凭据。如果服务器接收带凭据的请求,会用下面的HTTP头部来响应。

Access-Control-Allow-Credentials: true

如果发送的是带凭据的请求,但服务器的响应头中没有包含这个头部,那么浏览器就不会把相应交给JavaScript(于是,responseText 中将是空字符串,status的值为0,而且会调用onerror()事件处理程序)。另外,服务器还可以在Preflight响应中发送这个HTTP头 部,表示允许源发送带凭据的请求。

支持withCredentials属性的浏览器有Firefox 3.5+、Safari 4+和Chrome。IE10及更早版本都不支持。

带验证信息的请求DEMO

身份验证是Web开发中经常遇到的问题,在跨域请求中,默认情况下是不发送验证信息的。要想发送验证信息,需要进行withCredentials 属性,下面就是一个简单请求的例子:

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

孟宪会之AJAX跨域请求测试

var xhr = new XMLHttpRequest();

var url = 'http://dotnet.aspx.cc/RequestsWithCredentials.aspx';

function crossDomainRequest() {

document.getElementById("content").innerHTML = "开始进行请求……";

if (xhr) {

xhr.open('GET', url, true);

xhr.onreadystatechange = handler;

xhr.withCredentials = "true";

xhr.send();

} else {

document.getElementById("content").innerHTML = "不能创建 XMLHttpRequest。";

}

}

function handler(evtXHR) {

if (xhr.readyState == 4) {

if (xhr.status == 200) {

var response = xhr.responseText;

document.getElementById("content").innerHTML = "结果:" + response;

} else {

document.getElementById("content").innerHTML += "
执行状态 status:" + xhr.status;

}

}

else {

document.getElementById("content").innerHTML += "
执行状态 readyState:" + xhr.readyState;

}

}

// -->

点击“开始测试”,我们可以检测到下面的请求执行过程:

GET /RequestsWithCredentials.aspx HTTP/1.1

Host: dotnet.aspx.cc

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: zh-cn,zh;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html

Origin: http://www.meng_xian_hui.com:801

HTTP/1.x 200 OK

Date: Sun, 10 Jan 2010 14:12:26 GMT

Server: Microsoft-IIS/6.0

X-Powered-By: ASP.NET

X-AspNet-Version: 2.0.50727

Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801

Access-Control-Allow-Credentials: true

Set-Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145;path=/; HttpOnly

Set-Cookie: visit=1;expires=Sun, 10-Jan-2010 14:12:56 GMT;path=/

Cache-Control: no-cache

Pragma: no-cache

Expires: -1

Content-Type: text/html; charset=utf-8

Content-Length: 1

从上面的响应中可以看出,Cookie 是会随请求一起发送的。如果我们多次点击测试按钮,则可以看到请求和响应的结果是这样的:

GET /RequestsWithCredentials.aspx HTTP/1.1

Host: dotnet.aspx.cc

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: zh-cn,zh;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Referer: http://www.meng_xian_hui.com:801/CrossDomainAjax/RequestsWithCredentials.html

Origin: http://www.meng_xian_hui.com:801

Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145;visit=2

HTTP/1.x 200 OK

Date: Sun, 10 Jan 2010 14:13:58 GMT

Server: Microsoft-IIS/6.0

X-Powered-By: ASP.NET

X-AspNet-Version: 2.0.50727

Access-Control-Allow-Origin: http://www.meng_xian_hui.com:801

Access-Control-Allow-Credentials: true

Set-Cookie: visit=3;expires=Sun, 10-Jan-2010 14:14:28 GMT;path=/

Cache-Control: no-cache

Pragma: no-cache

Expires: -1

Content-Type: text/html; charset=utf-8

Content-Length: 1

注意 Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145; visit=2 这一行,访问计数器已经被一起发送到服务器。

4,IE8 中的实现方法(备注:需要开启IE浏览器安全配置internet安全区域的“通过域访问数据源”)

IE8已经开始支持跨域访问资源了,但是,IE8提供的功能还比较简单,可以进行简单的请求,下面是一个使用的例子:

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

孟宪会之AJAX跨域请求测试

var xhr = new XDomainRequest();

var url = 'http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx';

function crossDomainRequest() {

document.getElementById("content").innerHTML = "开始……";

if (xhr) {

xhr.open('GET', url);

xhr.onload = handler;

xhr.send();

} else {

document.getElementById("content").innerHTML = "不能创建 XDomainRequest";

}

}

function handler(evtXHR) {

document.getElementById("content").innerHTML = "结果:" + xhr.responseText;

}

// -->

另外,IE8的实现方法与其他浏览器不同。更多内容请参考 XDomainRequest对象。

加支付宝好友偷能量挖...

2013-8-13网络

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值