A great use of the public interface for AJAX submissions

Source: Internet
Author: User
Tags string to json

When the project framework is built, it writes the public interface of the AJAX submission, which is the error message that wants to unify the log and handle the Ajax return.

Today, but also to help me solve another problem: each time you open a page, there is an AJAX request will always call two times, so open the Chrome debugger, where the public Ajax submitted a breakpoint, view Chrome's call Stack, it is easy to find the source of two calls, Problem solved instantaneously.

Stick to my util.js:

/*! * Front-end JS Common method plugin * Copyright 2011-2015 zhangjh. * Licensed under MIT (https://github.com/firebata/skysport/blob/master/ LICENSE)*/(function ($) {    "Use Strict"; //Extending common methods$.extend ({sendrestfulajax:sendrestfulajax,//AjaxSendjsonajax:sendjsonajax, Strtojson:strtojson,//& connect string to JSON objectStrisempty:strisempty,//determine if the string is emptyStrisnotempty:strisnotempty//judge string is not empty    }); /** * * * @param _url path * @param _data Data * @param _type http method * @param _datatype data type * @param _dosuccess Successful callback function*/    functionSendrestfulajax (_url, _data, _type, _datatype, _dosuccess) {varType = Strisempty (_type)? ' POST ': _type; //var dataType = _type = = "undefined" | | $.trim (_datatype) = = "? ' JSON ': _datatype;        varSF = Strisempty (_dosuccess)?dosucess: _dosuccess; $.ajax ({URL: _url, data: _data, Type:type,//Datatype:datatype,success:sf, error:donotsucess}); }    /** * * * @param _url path * @param _data Data * @param _type http method * @param _datatype data type * @param _dosuccess Successful callback function*/    functionSendjsonajax (_url, _data, _dosuccess) {varSF = Strisempty (_dosuccess)?dosucess: _dosuccess; $.ajax ({url: _url, data:JSON.stringify (_data), type:"POST", DataType:"JSON", ContentType:"Application/json", SUCCESS:SF, error:donotsucess}); }    /*Bootbox.setlocale ("ZH_CN");*/    varDonotsucess =function(XMLHttpRequest, Textstatus, Errorthrown) {//Console.error (XMLHttpRequest);Bootbox.alert ("This operation failed.")); }    varDosucess =function(XMLHttpRequest, Textstatus, Errorthrown) {//Bootbox.alert (XMLHttpRequest);    }    /** * Convert the "&" concatenated string to JSON * @param str "&" concatenated String * @returns {Object} Json*/    functionStrtojson (str) {str= Str.replace (/&/g, "', '"); STR= Str.replace (/=/g, "': '")); STR= "({'" + str + "'})"; varobj =eval (str); returnobj; }    /** * Check string is empty * @param input String * @returns {Boolean}*/    functionStrisempty (input) {returnInput = = Undefined | | $.trim (input) = = "    }    /** * Check string is not empty * @param input String * @returns {Boolean}*/    functionStrisnotempty (input) {return!strisempty (input); }} (JQuery));

A great use of the public interface for AJAX submissions

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.