Extends the jquery Ajax request error mechanism to implement message feedback on the server.

Source: Internet
Author: User

Jquery enables us to develop Ajax applicationsProgramIt increases the efficiency and reduces many compatibility issues. However, after a long time, we can't do without it. For example, the simple jquery Ajax request encapsulation makes us forget the original XMLHTTPRequest object and its attributes and methods, and keeps us away from the truth.

In Ajax projects, data such as message prompts or message codes that require the server to return errors is often encountered. I have checked some help and solutions. Many websites use JSON data or scripts that return incorrect messages. In this way, jquery is used. there was a problem with Ajax (), jquery. the data parameters of the Ajax () callback function success (data) may be xmldoc, jsonobj, HTML, text, etc... it depends on our ype settings and mime. most of the time, we handle all the errors in a unified manner, regardless of whether the request is XML or JSON .... It is not only difficult to unify, but also prone to parsing errors.

Based on the prototyp framework practice, a jquery error extension is made.

Principle: The prototype principle is to write the server processing result status information in the header. This method not only ensures the reponse body is clean, but also adapts to the XML, JSON, HTML, and text responses.

The server only needsResponse. addheader ("Error-JSON","{Code: 2001, MSG: 'user settings is null! ', Script :''}");

Implementation: in order not to affect the original jquery. Ajax method, but not to damage the jquery Library source file, the following extensions are made,CodeEasy to understand:

;( Function ($) {
VaR Ajax = $. Ajax;
$. Ajax = Function (S) {
VaR Old = S. error;
VaR Errheader = S. errorheader | " Error-JSON " ;
S. Error = Function (Xhr, status, err) {
VaR Errmsg = Window [ " Eval " ] ( " ( "   + Xhr. getResponseHeader (errheader) +   " ) " );
Old (xhr, status, errmsg | Err );
}
Ajax (s );
}

}) (Jquery );

 

Usage:

Server Side: we are extending the error. If you want jquery. if an error occurs in Ajax cracking, the server must return a non-200 error code. The request header cannot be obtained because the Opera Browser faces more than 400 Error Codes. If you want to support opera, it is best to return 30 * error, which is the error range that opera can accept the header. If no packaging is made, you can separate the catch content.

Try   {
Context. response. Write (getjson (context ));
Throw NewException ("MSG");
}
Catch   {
Context. response. clearcontent ();
Context. response. statuscode =   300 ;
Context. response. addheader ( " Error-JSON " , " {Code: 2001, MSG: 'user settings is null! ', Script :''} " );
Context. response. End ();
}

Client:

$. Ajax ( {
URL: This . Ajaxurl,
Type: " Post " ,
Success: callback,
Error: Function (Xhr, status, errmsg) {
Alert (errmsg. Code+"<Br/>"+Errmsg. msg );
}
} );

Maybe not the best, but I think it is very convenient to use it. I forgot to write the new parameter errorheader: "error-JSON". The header key is configured according to your background settings.

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.