Ajax why




















Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. It means you fill out a form, hit submit, and get directed to a new page with new information from the server.

With AJAX, when you hit submit, JavaScript will make a request to the server, interpret the results, and update the current screen. In the purest sense, the user would never know that anything was even transmitted to the server. XML is commonly used as the format for receiving server data, although any format, including plain text, can be used.

A user can continue to use the application while the client program requests information from the server in the background. You can also add an always-different GET parameter, like a timestamp or random number see bypassing the cache.

Note: If the httpRequest variable is used globally, competing functions calling makeRequest can overwrite each other, causing a race condition. In the event of a communication error such as the server going down , an exception will be thrown in the onreadystatechange method when accessing the response status.

To mitigate this problem, you could wrap your if In the previous example, after receiving the response to the HTTP request we used the request object's responseText property , which contained the contents of the test.

Now let's try the responseXML property. First off, let's create a valid XML document that we'll request later on. The document test. Then in alertContents , we need to replace the line alert httpRequest.

Finally, let's send some data to the server and receive a response. Our JavaScript will request a dynamic page this time, test.

We'll also add a line to our event handler to get the user's data from the text box and send it to the makeRequest function along with the URL of our server-side script:. We need to modify makeRequest to accept the user data and pass it along to the server. The function alertContents can be written the same way it was in Step 3 to alert our computed string, if that's all the server returns.

However, let's say the server is going to return both the computed string and the original user data. So if our user typed "Jane" in the text box, the server's response would look like this:.



0コメント

  • 1000 / 1000