jquery Ajax return value and no return value problem

Source: Internet
Author: User
Tags json


jquery Ajax return values are many types, such as the Html,text,json,xml type, we can be in the AJAXP processing is directly using anonymous functions to directly obtain, the following I collate a jquery Ajax return value of the example for you to reference.



The common Ajac in jquery are $.ajax (), $.post, $.get (), $.load ().



Example


The code is as follows

$.post ("test.jsp",
{name: ' Cssrain ', Time: ' 2008/01/21 '},//data to be passed
function (data) {
Alert ("Returned data: +");
}
)


The return value of this is data, as long as we can access the return result data in function (data) {}.



Example $.load ()


The code is as follows

$ ("#loadajax"). Load ("http://www.111cn.net. Post",

function (ResponseText, textstatus, XMLHttpRequest) {

this;//here. This point is the current DOM object, that is $ (". Ajax.load") [0]
});

<div Id=loadajax> This will receive the return value of the load. Oh </div>


Example


  code is as follows

$.ajax ({
Type: ' Get ',//using the Get method to access the background
DataType: "JSON",//back to JS On format data
URL: "Backhandler.ashx",//background address to access
Data: "pageindex=" + pageindex,//to send
Complete:function () {$ ( "#load"). Hide ();},//ajax loading prompt
Success:function (msg) {//msg for returned data, where to do data binding
var data = msg.table;
$.each (data, function (I, n) {
var row = $ ("#template"). Clone ();
Row.find ("#OrderID"). Text (n. Order ID);
row.find ("#CustomerID"). Text (n. Customer ID);
Row.find ("#EmployeeID"). Text (n. Employee ID);
Row.find ("#OrderDate"). Text (ChangeDate (n. Order date));
if (n. Ship date!== undefined) row.find ("#ShippedDate"). Text (ChangeDate (n. Ship date));
Row.find ("#ShippedName"). Text (n. Shipper name);
Row.find ("#ShippedAddress"). Text (n. Shipper's address);
Row.find ("#ShippedCity"). Text (n. Shipper city);
Row.find ("#more"). HTML ("<a href=orderinfo.Aspx?id= "+ N. Order ID +" &pageindex= "+pageindex+" >&nbsp; More</a> ");
Row.attr ("id", "Ready");//change the ID of the row that binds the data
Row.appendto ("#datas");//Add to Template's container
});


This is the return JSON format.



jquery Ajax no return value



In the Ajax method of jquery, after passing the parameter, the callback is judged to have success and error two kinds of cases.



Sometimes, without the need to return the value of the case, throw in template format, set datatype: "JSON", parameter; At this point, when the Ajax pass value is correct, there are 200 return to the success of the status of the error of the special case.



I didn't notice the setting of the return value data type of the Ajax method before. In the absence of a return parameter, you typically do not need to set the data type of the returned value. If the setting is wrong, the error is generally incorrect. At this point, simply cancel DataType: "JSON",



Ajax method Correct template with no return value:


The code is as follows

$.ajax ({

Type: "Post",

URL: "index.php",

Data: "Id=" +uid,

Success:function () {

Alert (1);

},

Error:function () {

Alert (0);

}

});


Example



jquery's Ajax authentication username. e-mail, verification code


  code is as follows

function Check_email ()
{
var check_email = $ ("#reg_mail"). Val ();
var reg =/^ ([a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + ((/.[ a-za-z0-9_-]{2,3}) {1,2}) $/;
Flag = reg.test (Check_email);
if (flag)
{
var email_value = $ (' #reg_mail '). Val ();
$.get ("[var.base_url]register.php", {check_name: " Email ", check_value:email_value,async:false},function (JSON) {
//$ (' #res_mail '). HTML (JSON);
if (json = =" OK ")
{
$ (' #res_mail '). HTML ("<font color= ' green ' font-size= '" ><b> this email can be registered!) </b></font> ");
return true;
}else{
$ (' #res_mail '). html (' <font color= ' red ' font-size= ' ><b> This email has been registered! </b></font> ");
return false;
}
});
}else{
$ ("#res_mail"). HTML ("<font color= ' red ' font-size= '" ><b> Please enter the correct email address!) </b&gT;</font> ");
return false;
}
}


That's how it started. But always cannot get to the returned state true or false returns a undefined a lot of data. A post on the CSDN is classic:


The code is as follows

var Boolean = false;
$.get (data) {//Understand this is not difficult, as long as you know that the methods in jquery are returned by the jquery object or the jquery-specified object. url,null,function
jquery's Get, post, and so on Ajax methods are asynchronous interactive mode, so the Getting method has not been completed when the return, then the BL is your definition of BL = false, so always return false;
So to put back the correct value of BL, you have to change the Get method. Generally do not do return value processing in AJAX methods.
You can use $.data ("BL", BL), save your value, and then use $.data ("BL") to take a value.
if (Data.indexof ("true") >=0) {
$ ("#mid"). HTML ("landing success");
BL = true;
$.data ("BL", BL);
}
else{
$ ("#mid"). HTML ("User name or password error");

BL = false;
$.data ("BL", BL);
}
});
return BL;


This will allow you to get to the returned state. Store the value with the data method. And then get. There's another way to do that.


The code is as follows

$.get (data) set transmission mode to synchronous transmission (url,{async:false},function)


The final modification function is as follows. Test OK.


  code is as follow

function Check_email ()
{
var check_email = $ ("#reg_mail"). Val ();
var reg =/^ ([a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + ((/.[ a-za-z0-9_-]{2,3}) {1,2}) $/;
Flag = reg.test (Check_email);
if (flag)
{
var email_value = $ (' #reg_mail '). Val ();
$.get ("[var.base_url]register.php", {check_name: " Email ", check_value:email_value,async:false},function (JSON) {
//$ (' #res_mail '). HTML (JSON);
if (json = =" OK ")
{
$ (' #res_mail '). HTML ("<font color= ' green ' font-size= '" ><b> this email can be registered!) </b></font> ");
Tamp_email = true;
$.data ("Tamp_email", Tamp_email);
//$ (' #sub_reg '). attr ("disabled", false);
}else{
$ (' #res_mail '). html (' <font color= ' red ' font-size= ' ><b> This email has been registered! </b></font> ");
Tamp_email = false;
$.data ("Tamp_email", Tamp_email);
//$ (' #sub_reg '). attr ("Disabled", true);
}
});
return tamp_email;
}else{
$ ("#res_mail"). HTML ("<font color= ' red ' font-size= '" ><b> Please enter the correct email address!) </b></font> ");
return false;
}
}

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.