jquery 回调函数 返回值
场景:ajax,jquery,回调函数怎么接收返回值
ajax,jquery,回调函数如何接收返回值?
post之后,回调函数是如何接受返回值的?
------解决方案--------------------
我给你一个非常完整的例子,注释也很全,自己研究吧
ajax,jquery,回调函数如何接收返回值?
- JScript code
$(document).ready(function() { // generate markup $("#rating").append("Please rate: "); for ( var i = 1; i <= 5; i++ ) $("#rating").append("<a href='#'>" + i + "</a> "); // add markup to container and apply click handlers to anchors $("#rating a").click(function(e){ // stop normal link click e.preventDefault(); // send request [color=#FF0000][b]$.post("aaa.jsp", {rating: $(this).html()}, function(xml) {[/b] [/color] // format and output result alert(xml); $("#rating").html( "Thanks for rating, current average: " + $("average", xml).text() + ", number of votes: " + $("count", xml).text() ); }); }); });
post之后,回调函数是如何接受返回值的?
------解决方案--------------------
我给你一个非常完整的例子,注释也很全,自己研究吧
- JScript code
//用户名校验的方法 //这个方法使用XMLHTTPRequest对象进行AJAX的异步数据交互 var xmlhttp; function verify(){ //1.使用dom的方式获取文本框中的值 //document.getElementBuId("userName")是dom中获取元素节点的一种方法,一个元素节点对应HTML页面中的一个标签,如果<input> //.value可以获取一个元素节点的value属性值 var userName = document.getElementById("userName").value; //2.创建XMLHttpRequest对象 //这是XMLHttpRequest兑现使用中最为复杂的一步 //需要这对IE和其他类型浏览器建立这个对象的不方式写不同的代码 if(window.XMLHttpRequest){ //针对FireFox,Mozillar,opera,safari,IE7,IE8 xmlhttp = new XMLHttpRequest(); //针对某些特定版本的mozillar浏览器的bug修正 if(xmlhttp.overrideMimeType){ xmlhttp.overrideMimeType("text/xml") } }else if(window.ActiveXObject){ //针对IE5,IE5.5,IE6(IE7,IE8) //两个可以用于创建XMLHTTPRequest对象的控件名称,保存在一个js数组中 //排在前面的版本较新 var activexName =["MSXML2.XMLHTTP","Microsoft.XMLHTTP"]; for(var i=0;i<activexName.length;i++){ try{ //取出一个控件名进行创建,如果创建成功就终止循环 //如果创建失败,抛出异常,然后继续循环,继续尝试创建 xmlhttp = new ActiveXObject(activexName[i]); break; } catch(e){ } } } // 确认XMLHttpRequest对象创建成功 // if(!xmlhttp){ // alert("XMLHttpRequest对象创建失败!"); // return; // }else{ // //alert(xmlhttp); // alert(xmlhttp.readyState); // } //2 注册回调函数 //注册回调函数时只需要函数名不要加括号 //我们需要注册的是函数名称,如果加上括号,就把函数的返回值给注册上了,这是错误的 xmlhttp.onreadystatechange = callback; //3设置连接信息 //第一个参数表示http的请求方式。支持所有http的请求方式。主要使用get和post //第二个参数表示请求的url地址,get方式的参数也在url中 //第三个参数表示采用异步还是同步方式交互,true表示异步 //GET方式请求的代码 //xmlhttp.open("GET","AJAXServer?name="+userName,true); //POST方式请求的代码 xmlhttp.open("POST","AJAXServer",true); //POST方式需要自己设置http的请求头 xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //POST方式发送数据 xmlhttp.send("name="+userName); //4 发送数据,开始和服务器端进行交互 //同步方式下,send这句话会在服务器设置回来后才执行完 //异步方式,send这句话执行晚会立即完成 //GET方式 //xmlhttp.send(null); } //回调函数 function callback(){ // alert(xmlhttp.readyState); //5接收响应数据 //判断对象的状态是交互完成 if(xmlhttp.readyState==4){ //判断http的交互是否成功、 if(xmlhttp.status==200){ //获取服务器端返回的数据 //获取服务器端输出的纯文本数据 var responseText = xmlhttp.responseText; //将数据显示在页面上 //通过dom方式找到div标签所对应的元素节点 var divNode = document.getElementById("result"); //设置元素节点中的html内容 divNode.innerHTML=responseText; }else { alert("出错了"); } } }
------解决方案--------------------
楼上很厉害
注释写的真好!佩服!
------解决方案--------------------
发送
$.ajax({
type: "POST",
url: url,
data: data,
dataType: "xml",
success: function(xml){ show(xml) }
});
处理回调
function show(xml) {
var obj = xml.getElementsByTagName("obj");
...
}
LZ从一片代码中复制出一堆怪怪的- -
下次花点时间整理好再发吧。
------解决方案--------------------
- Java code
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setHeader("Cache-Control", "no-store"); //HTTP1.1 response.setHeader("Pragma", "no-cache"); //HTTP1.0 response.setDateHeader("Expires", 0); //prevents catching at proxy server PrintWriter out = response.getWriter(); String userId = request.getParameter("userId"); String oldPassword = request.getParameter("oldPassword"); if(userId != null && !userId.trim().equals("")) { User user = UserMgr.getInstance().loadByUserId(userId); if(user.getPassword().equals(oldPassword)) { out.write("valid"); } else { out.write("invalid"); } } out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
------解决方案--------------------
学习。。。
------解决方案--------------------
------解决方案--------------------
1楼写的是尚学堂的
------解决方案--------------------
------解决方案--------------------
$get("aa.jsp",null,test);
function test(data)
{
//这个就是回调函数,另外声明就可以了,data 就是接受参数的值你可以弹个框试试
alert(data);
}
------解决方案--------------------
学习,学习!