Ajax processing error (VI)

Source: Internet
Author: User

There are two types of errors to be aware of when using AJAX, and the difference between them stems from a different perspective.
One, the first type of error is seen from the perspective of the XMLHttpRequest object: some factors such as blocking the request sent to the server, such as DNS cannot resolve the host name, the connection request is denied, or the URL is invalid.
The second type of problem is the problem seen from the application perspective rather than the XMLHttpRequest object. They occur when a request is successfully sent to the server, the server accepts the request, processes it, and generates a response, but the response does not point to what you expect. For example, if the URL you requested does not exist, this type of problem will occur.

One, handling settings error

The first type of problem that needs to be addressed is passing the wrong data to the XMLHttpRequest object, such as malformed URLs, which are extremely easy to generate when generating URLs based on user input. This is an error that prevents the request from executing, and the XMLHttpRequest object throws an error when such an event occurs. This means that you need to use a Try...catch statement to surround the code that sets the request, as follows:

try{      ...    httpRequest.open("GET","http://");    ...    httpRequest.send();}catch(error){    displayErrorMsg("try/catch",error.message)}

The catch clause gives you a chance to recover from the error. You can choose to prompt the user for a value, fallback to the default URL, or simply discard the request.

Second, processing request error

The second type of error occurs when a request has been generated, but something else is wrong. Register an Error event listener and the browser will send an event object to your listener function. Httprequest.onerror=handleerror;

function handleError(e){    displayErrorMsg("Error event",httpRequest.status+httpRequest.statusText)}

When such errors occur, the degree of information you can obtain from the XMLHttpRequest object depends on the browser, and, unfortunately, in most cases you will get the status of the value 0 and the blank statustext value.
The second problem is that the URL and the script that generates the request have a different source, which is not allowed by default. You can usually only send AJAX requests to the same-origin URL that is loaded into the script. When the browser reports this problem, Bu ' Jin throws an error or triggers an error event, and the browser handles different ways. Different browsers also check the source at different points in time, which means that you may not always be able to see the browser highlighting the same issue. You can use cross-site resources (CORS cross-origin Resource sharing) to bypass the same-origin restrictions.

Iii. Handling Application Errors

The last type of error occurs when the request finishes (from the perspective of the XMLHttpRequest object), but does not return the data you want. There is no error in the process itself (because the request was completed successfully), you need to determine what happened based on the status property. When you request a non-existent document, you get 404 of this status code, it means that the server cannot find the requested document, you can see how I handle 200 (meaning OK) other than the status code:

if(httpRequest.status==200){    target.innerHtml=httpRequest.responseText;}else{    document.getElementById("statusmsg").innerHtml="Status:"+httpRequest.statusText;}

In this example, I simply show the status and StatusText values. In a real application, you need to recover in a useful and meaningful way (such as displaying alternate content or alerting users to problems, depending on which is more appropriate for the application)

Ajax processing error (VI)

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.