JQuery Ajax Instance (i)

Source: Internet
Author: User
Tags json return string

There are many types of ajax implementation methods in jquery, such as: load, jQuery.get, jQuery.post, jQuery.getScript, jQuery Ajax events, jQuery.ajaxSetup, etc.

First, the general format of $ .ajax
$ .ajax ({

     type: 'POST',

     url: url,

    data: data,

    success: success,

    dataType: dataType

});

Second, $ .ajax parameter description

Parameter Description
url is required. Specifies which URL to send the request to.
data Optional. Map or string value. Specifies the data to be sent to the server along with the request.
success (data, textStatus, jqXHR) Optional. Callback function executed when the request is successful.
dataType
Optional. Specifies the data type of the expected server response.

Perform smart judgment (xml, json, script or html) by default.

Three, $ .ajax need to pay attention to some places:

  There are three main ways of 1.data, html splicing, json array, serial form (serialize ()); specified by dataType, intelligent judgment is not specified.

  2. $. Ajax only submits the form in text mode. If the asynchronous submission contains <file> uploading, it will not pass, you need to use $ .ajaxSubmit of jquery.form.js


Fourth, the Ajax case
//1.$.ajax asynchronous request with json data

var aj = $ .ajax ({url: 'productManager_reverseUpdate', // jump to action data: {selRollBack: selRollBack, selOperatorsCode: selOperatorsCode, PROVINCECODE: PROVINCECODE, pass2: pass2}, type: 'post', cache: false, dataType: 'json', success: function (data) {if (data.msg == "true") {// view ("Successfully modified!"); alert ("Successfully modified!"); window.location.reload ();} else {view (data.msg);}}, error: function () {// view ("Exception!"); alert ("Exception!");}}); //2.$. asynchronous request for ajax serialized form content as a string function noTips () {var formParam = $ ("# form1"). serialize (); // serialized form content is a string , url: 'Notice_noTipsNotice', data: formParam, cache: false, dataType: 'json', success: function (data) {}});} //3.$.ajax asynchronous request for splicing var yz = $. ajax ({type: 'post ', url:' validatePwd2_checkPwd2? password2 = '+ password2, data: {}, cache: false, dataType:' json ', success: function (data) {if (data.msg == "false") // The server returns False, change the value of validatePassword2 to pwd2Error, which is asynchronous, you need to consider the return time {textPassword2.html ("<font color = 'red'> The business password is incorrect! </ font> "); $ (" # validatePassword2 "). val (" pwd2Error "); checkPassword2 = false; return;}}, error: function () {}}); //4.$.ajax stitching data $ .Ajax ({url: '<% = request.getContextPath ()%> / kc / kc_checkMerNameUnique.action', type: 'post', data: 'merName =' + values, async: false, // Default is true asynchronous error: function () {alert ('error');}, success: function (data) {$ ("#" + divs) .html (data);}});


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.