The method of extending jquery uniformly handles exception error messages generated during AJAX calls

Source: Internet
Author: User

Because the project is a lot of practical .PosTSquareMethod,andtheSquareMethodTacitrecognizeNothePleaseAskof thedifferentoftenintoLinePlaceManagement,afterperiodandto bePlaceManagementtheAskQuestions. beforeperiodof theSolutiondecisionSquareCaseis awill bemakewith The. Post requests are transformed into $.ajax requests, but due to the fact that the implementation of the two methods (required parameters) is quite different, the changes are cumbersome!

During an attempt to use the definition "AJAX request error occurs when executing the function (document). Ajaxerror ())"
The way to solve the problem, but the scope of the impact is too large, the long polling error is also captured, causing the page to open the dialog box that pops up the system exception.

So here's the technique of extending the jquery approach to solve the problem:

/* Extended Jquery.post method * is used to support error handling, the original use of $.post only need to change to $.postex, do not need to make other changes; * If you need to handle exceptions separately, set the fourth parameter to an exception handler; */JQuery. Extend({  postex:function (Requesturl, SendData, Successcallback, Errorcallback) {if (type        of Errorcallback = = = ' undefined ') {errorcallback = Ajaxerrorex; }$. Ajax({  URL: requesturl, Data:senddata, type: ' post ', Cache:false, DataType: ' json ', timeout: 30000, SUCCESS:SUCC Esscallback, Error:errorcallback  }); }});

Unified Exception Handling: Original use .PosTof thetoSquareonlyneedto beChangeto be . Postex, no other changes are required;
Handling Exceptions individually: If you need to handle exceptions separately, set the fourth parameter to the exception handler;

The method of extending jquery uniformly handles exception error messages generated during AJAX calls

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.