CHAPTER 9 THE AJAX APPROACH TO BROWSER (Http web server)
CHAPTER 9 THE AJAX APPROACH TO BROWSER SCRIPTING 269 SECURITY For security reasons, you can t use Ajax to request content from a domain outside of the current one. This is referred to as the Ajax sandbox. If you re running the web page on http://www.apress.com, you can only request from that domain. As you re working within a sandbox, you can make server-side requests without a domain name. For example, I could request XML from a server-side file using this code: xmlhttp.open(”GET”, “/bin/getXML.aspx”, true); You could also include a parameter in this method call: xmlhttp.open(”GET”, “/bin/getXML.aspx?contactName=” + escape(cName), true); The send() method can pass information with a request. You ll probably use it to POST information that filters the returned content. You send data in variable pairs: xmlhttp.send(’var1=val1&var2=val2&var3=val3′); Make sure you use escape to encode the values that you send. You can also use the value null to indicate that you re not sending variables. If you re posting data, you need to change the MIME type of the request: xmlhttp.setRequestHeader(’Content-Type’, ‘application/x-www-form-urlencoded’); You can use an onreadystatechange event handler as I did in the previous chapter to check the value of the readyState property. The event handler can determine one of the following five values: 0: The request is not yet initialized. This occurs before calling the open() method. 1: The request is initialized but not sent. This occurs before calling the send() method. 2: The request has been sent and is being processed. 3: The request is being processed but hasn t been finished. 4: The response is completed. You can access the information with the responseText or responseXML property. The ready states don t work exactly the same way on each type of web browser. If you track the value of the readyState property, you might see different results in Safari compared with IE. Each time the readyStateproperty changes, the application calls the event handler function. The code responds when the readyState equals 4, indicating that the response is complete:
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.