Ajax hacks-hack 5. Get normal string

Source: Internet
Author: User
Tags format object button type end string string format stock prices
Ajax| string Ajax hacks-hack 5. Get normal string

The normal string is used to manage weather information, stock prices, and other times when you don't use XML.

The request object can sometimes be applied to Web applications without using XML:request.responseText. This hack the user by selecting a stock symbol and then obtaining the price of the stock in a string format from the server. The price to get the stock in the next hack is in numbers format.

First look at the page's HTML code, which introduces the source file Hack9.js


"Http://www.w3.org/tr/1999/rec-html401–19991224/strict.dtd" >

Stock prices

Javascript:void%200>
"Getstockprice (This.stSymbol.value); return false" >

Enter stock symbol:



get stock price

/form>

Figure 1-5 is the display of the page in Firefox. The user enters the ticker symbol such as "GRMN", then clicks on the Stock price button, and the program sends the ticker message to the server, then gets the corresponding price from the server in the format of the string and displays it. The

Function Getstockprice is a request-handling function. The function obtains the stock code from the page and obtains the stock price returned from the server (it is the request object that communicates with the server, obtains the real time stock price). The code is as follows:

var request;

var symbol; Save ticker symbol

function Getstockprice (sym) {

symbol=sym;

if (sym) {

var url= "http://www.parkerriver.com/s/stocks?symbol=" +SYM;

HttpRequest ("Get", url,true);

}

}

//xmlhttprequest event handler

function Handleresponse () {

if (request.readystate = 4) {

if (Request.status =) {

/* Grab The result as a string */

var stockprice = request.responsetext;

var info = "«the Price is: $" +stockprice+ "»";

document.getElementById ("Stprice". style.fontsize= "0.9em";

document.getElementById ("Stprice". Style.

BackgroundcoloR= "Yellow";

document.getElementById ("Stprice". Innerhtml=info;

} else {

alert ("A problem occurred with communicating between" +

"The XMLHttpRequest object and th E server program. ";

}

} End outer if

}

/* View the Code of the HttpRequest () function in the hack #1; omit */

here Function Getstockprice is called inside the function HttpRequest, which is responsible for setting the Request object. If the reader has seen the first few hacks of this chapter, it will be understood that handleresponse encapsulates more important actions.

Function Handleresponse () {

if (request.readystate = = 4) {

if (request.status =) {

/*grab The result as a string*/

var stockprice = request.responsetext;

var info = "«the Price is: $" +stockprice+ "»";

document.getElementById ("Stprice". style.fontsize= "0.9em";

document.getElementById ("Stprice". style.backgroundcolor= "Yellow";

document.getElementById ("Stprice". InnerHTML =info;

} else {

alert ("A problem occurred with communicating between the XMLHttpRequest object and the serveR program. ";

}

} End outer if

}

When the request completes (that is, the Request.readystate value is 4), the HTTP response status is 400 (indicates the request succeeded). The code uses Request.responsetext to get the server's response. The code then uses the DOM to display the relevant information.

document.getElementById ("Stprice"). style.fontsize= "0.9em";


document.getElementById ("Stprice". style.backgroundcolor= "Yellow";

document.getElementById ("Stprice". InnerHTML =info;

This hack is about how to obtain the response information of the server through Request.responsetext, and what format information to obtain depends on the format of the information that the server responds to.

<

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.