16 12
发新话题
打印

[AJAX应用] Ajax Hacks-hack9 深入了解HTTP Response

Ajax Hacks-hack9 深入了解HTTP Response

Ajax Hacks-hack9 深入了解HTTP Response

HTTP响应头是描述性的信息,符合HTTP 1.1 协议规范,web服务器向请求端发送精确的页面或数据。如果你已经使用XMLHttpRequest对象编过程(在本章的前面讨论过),你应该知道request.status属性是从服务器返回的响应状态码。它是用来检测HTTP响应状态的重要值。


装态值包括200(请求成功)、404(请求的URL不存在,或页面不存在)500(服务器内部错误)。


然而,你还想看到更多的其他优选请求的响应头信息,例如与响应相关的web服务器软件类型(服务器请求头),或者响应的content类型(Content-Type)。本hack请求用户输入的一个URL。当用户点击输入框以外的部分时,浏览器会显示该URL的响应头信息。作为一个ajax应用,页面是不会出现刷新情况的。


请求对象方法只返回可用的响应头的一个子集,包括Content-Type, Date, Server,和Content-Length.


HTML代码如下:


http://www.w3.org/TR/1999/REC-html401–19991224/strict.dtd”>



Find out the HTTP response headers when you "GET" a Web page

javascript:void%200>


Enter a URL:



::press tab when finished editing the

field::

Figure 1-13 shows the page in the Safari browser.

Figure 1-13. Scoping the response


程序预先使用http://www.parkerriver.com/s/sender?name=guest来填充该text。


当用户输入URL以后点击Tab键,或点击输入框外的部分时,输入框的onblur事件被激发。事件处理函数是getAllHeaders,该函数传递用户输入的RUL进入request对象。request对象向此URL发送请求,并向web页面返回的响应头。


下面的代码是文件hack7.js。显示代码以后,将解释一下如何显示服务器响应头信息。其余的可以参考本章其他hack。


var request;

var urlFragment=“http://www.parkerriver.com/s/sender?name=guest”;


function getAllHeaders(url){

httpRequest(“GET”,url,true);

}

//function for XMLHttpRequest onreadystatechange event handler function handleResponse( ){ try{ if(request.readyState == 4){ if(request.status == 200){ /* All headers received as a single string */ var headers = request.getAllResponseHeaders( ); var div = document.getElementById(“msgDisplay”; div.className=“header”; div.innerHTML=”

"+headers+""; } else { //request.status is 503 if the application isn‘t available; //500 if the application has a bug alert(request.status); alert(“A problem occurred with communicating between ”+ “the XMLHttpRequest object and the server program.”; } }//end outer if } catch (err) { alert(“It does not appear that the server is ”+ “available for this application. Please”+ “ try again very soon. \\nError: ”+err.message);

}

}


/* Initialize a request object that is already constructed */

function initReq(reqType,url,bool){

try{

/* Specify the function that will handle the HTTP response */

request.onreadystatechange=handleResponse;

request.open(reqType,url,bool);

request.send(null);

} catch (errv) {

alert(

“The application cannot contact the server at the moment. ”+

“Please try again in a few seconds.” );

}

}


/* Wrapper function for constructing a request object.

Parameters:

reqType: The HTTP request type, such as GET or POST.

url: The URL of the server program.

asynch: Whether to send the request asynchronously or not. */

function httpRequest(reqType,url,asynch){

//Mozilla-based browsers

if(window.XMLHttpRequest){

request = new XMLHttpRequest( );

} else if (window.ActiveXObject){

request=new ActiveXObject(“Msxml2.XMLHTTP”;

if (! request){

request=new ActiveXObject(“Microsoft.XMLHTTP”;

}

}

//the request could still be null if neither ActiveXObject

//initialization succeeded

if(request){

initReq(reqType,url,asynch);

} else {

alert(“Your browser does not permit the use of all ”+

“of this application‘s features!”;

}

}


令人感兴趣的部分是handleResponse函数。该函数调用request对象的getAllResponseHeaders方法,该方法返回所有的可用的响应头信息,预格式为string。开发者更喜欢这些值作为JSON格式返回,而不是作为一个单一的字符串返回。


为取得一个头信息,也可以使用request.getResponseHeader方法。例如request.getResponseHeader("Content-Type")可以得到响应头的Content类型:


接下来代码将取得div元素,并在其中显示头信息:

if(request.status == 200){ /* All headers received as a single string */ var headers = request.getAllResponseHeaders( ); var div = document.getElementById(“msgDisplay”; div.className=“header”; div.innerHTML=”

"+headers+""; }...

为能向信息显示提供CSS格式,代码设置了div的className属性,这个类已经预先定义了,代码如下:


div.header{ border: thin solid black; padding: 10%;

font-size: 0.9em; background-color: yellow}

span.message { font-size: 0.8em; }


以这样的方式,代码会动态地将一个div连向一个特定的CSS类,这个类是单独定义的。这种策略帮助DOM编程者描述区分。最后div的innerHTML属性被设置进返回头值里边。
这世界总有你不明白

TOP

水泥生产线

郑州环球重工机械有限公司专业生产水泥生产线设备选矿生产线,制砂生产线碎石生产线

TOP

TOP

TOP

TOP

孙田莉子辣舞酷似“芙蓉” 造型野性十足



   


腾讯娱乐讯:2008年4月24日,北京,昨日(4月23日)国内首部以拉丁舞为主题的电影《舞动心灵》在京拍摄已接近尾声。片中主演之一孙田莉子在拍摄现场倾力施展舞技,现场表现十分沉着稳健,这对于一个首次触电的新人来说难能可贵。昨天,记者来到电影《舞动心灵》拍摄现场探班时,剧组正在拍摄一场女主角小曼秀舞的场景,这自然难不倒舞蹈专业出身的莉子。在导演张挺的安排下频频摆出高难度动作,奔放张扬,显得野性十足。记者注意到,大部分镜头莉子都是一遍就过。她告诉记者,别看现在她在镜头前显得很“风光”,在当初学舞蹈时,可是吃了不少苦头,经常从早上8点练到晚上12点。


据介绍,莉子现在是中央戏剧学院影视表演专业的学生,她10岁就以专业课全国第一名的成绩考上北京舞蹈学院附中,05年8月,获CSARA国际体育舞蹈竞赛证书,拉丁舞艺术院校专业组第一名。06年全国青少年体育舞蹈锦标赛十项全能第一名,在体育舞蹈界被冠以“90后拉丁小天后”的称谓。在北京舞蹈学院毕业前夕被入选到“国家青年队”。90年出生今年刚满18岁的莉子表示“我觉得人必须要挑战自己,我还年轻,不想一生只做一件事。”

TOP

广东进口轴承、广东FAG轴承、广东FAG进口轴承

TOP

丽妍堂真轻松排汗

正品丽妍堂真轻松排汗运动机厂家直销13738963879原厂正品--丽妍堂真轻松排汗运动机,蒸美康排汗运动机厂家直销,优惠直销.订购热线0579-87368390,运动排汗,强身健体,采用薰蒸的原理,集排毒,减压,健体,美容养颜,减肥,黄金睡眠功效为一体.每天蒸一蒸,跟疲劳,斑点,毒素说再见. 丽妍堂排汗,丽妍堂排汗机好不好,丽妍堂排汗机怎么样,丽妍堂排汗机好吗,丽妍堂真轻松排汗机 丽妍堂排汗运动机,丽妍堂排汗机的价格,丽妍堂真舒服排汗机,丽妍堂排汗机好不好,丽妍堂排汗机怎么样,丽妍堂排汗机好吗,丽妍堂排汗机价格,丽妍堂蒸汽排汗机。公司网站:www.hd273.com

TOP

age of conan

There is much controversy today about whether television has destroyed communication among family and friends. Some people argue that the communication is baffled by television. Others criticize that television improve the relationship of family and friends. I totally agree with the latter one. <a href=http://http://www.fast-wowgold.com/age-of-conan-gold.htm>cheap AOC gold</a>
Watching TV is a time for the whole family to stop from whatever they are doing to get together. While watch TV, they start to talk about the content of the movie, which is also a kind of communication. Imagining the TV disappears,<a href=http://http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan account</a>
we would do reading in rooms, while our family would do laundry, do washing dish or going shopping. It simply provides a chance for us to stay together. <a href=http://http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan power leveling</a>
In addition, the TV supplies us with much information, which we have not known in our daily lives. Some of the constructive movies show on TV, like a family story, a love story, or so on, reinforce the relationship of a family and friends.<a href=http://http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan power leveling</a> At last, it is a kind of recreation that a family or friends can choose to have. Communication can be undertaken by many means, for example, going shopping together, entering a bar, talking intimately about ourselves.

TOP

高手啊!

支持!支持!

开店 创业 投资 加盟 连锁

TOP

中国性病治疗中心

TOP

NACHI NACHI轴承 NACHI进口轴承

我顶你兄弟我使劲顶
NACHI进口轴承
NACHI轴承
NACHI专卖
NACHI

TOP

Poor college students given helping hand

The Ministry of Education pledged on Friday that poverty will no longer force students to drop out of college or university.

Every public college and university has openedworld of warcraft gold a "green passage" to let poverty-stricken freshmen enroll and begin their studies before paying tuition fees, ministry spokeswoman Xu Mei told a press conference.

This year's college and university enrollment is currently taking place.

All students had previously been required to pay tuition fees before commencing their studies.

More funds will be earmarked to Dofus Kamas assist students, especially those from areas hit by the May 12 earthquake, the spokeswoman said.

By July 18, the China Education Development Foundation had received donations totaling 150 million yuan ($22 million) to assist education in quake-hit areas.

"So far, more than 55 million yuan has been spent on the living and education allowances of students in quake-hit areas in Sichuan, dofus kamasGansu and Shaanxi provinces," Zhang Zhongyuan, secretary-general of the foundation, told the press conference.

"Plans to rebuild schools in the affected areas have been drawn up and reconstruction work will begin soon," Zhang said.

The donations will also cover the traveling expenses of students who have to study in other places due to the quake, he said.

The ministry printed 6 million booklets last month featuring its financial aid policy for students and has urged every college to attach it to each admission notice sent to would-be students.

The government has widened its financial aid system for students since May last year to provide more scholarships, stipends, student loans and emergency financial allowances for poverty-stricken students.

Students have also been offered campus work FFXI Gilopportunities and free education in teachers' colleges, and tuition fees have been cut for students from special groups, such as the disabled or ethnic minorities.

The government spent 27.3 billion yuan last Age of Conan Goldyear to aid college students, up 49 percent from the previous year, statistics showed.

Around one-fifth of the 20 million students studying at public and private universities last year were from poverty-stricken backgrounds, official figures showed.

TOP

FAG

谢谢分享

一位经济学家回到他的母校,skf轴承并对眼下的考试题目很感兴趣。fag轴承于是他请曾经教过他的老师拿出考题。 nsk轴承使他大吃一惊的是,现在的考题与他十年前答的题一模一样。 进口轴承他问老师为什么会这样。timken轴承老师回答说:“问题虽然没变,但答案变了。”
nsk

TOP

青岛轴承 青岛NSK轴承 青岛NSK进口轴承

TOP

 16 12
发新话题