原始js:
1 function update_mess(){ 2 var account_name = $("#account").val(); 3 var xmlhttp; 4 if(window.XMLHttpRequest){ 5 xmlhttp = new XMLHttpRequest; 6 } 7 else{ 8 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 9 } 10 xmlhttp.onreadystatechange = function(){ 11 if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ 12 var json = eval("("+ xmlhttp.responseText +")"); 13 if(json.returnedjson.infostatus == 'T'){ 14 alert(json.returnedjson.infomsg); 15 location.reload(); 16 } 17 else{ 18 alert(json.returnedjson.infomsg); 19 } 20 } 21 } 22 var str = "account_id=" + account_id; 23 xmlhttp.open("post", "/update_mess_action", true); 24 xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 25 xmlhttp.send(str); 26 }
ajax提交方式比js原始的提交方式更为简介,具体如下:
1 $.ajax({ 2 type:"post", 3 url:"find_password_action?account_name="+ account_name + "&account_answer1=" + account_answer1 + "&account_answer2=" + account_answer2, 4 success:function(date){ 5 var json = eval("("+ date +")"); 6 if(json.returnedjson.infostatus == "T"){ 7 //alert(json.returnedjson.infomsg); 8 $("#mymodal1").modal("toggle"); 9 } 10 else{ 11 alert(json.returnedjson.infomsg); 12 } 13 }, 14 error:function (date){ 15 alert("找回密码失败!"); 16 }, 17 });
两种差不多,但是jquery的ajax更加灵活,对于post没有那么多的限制。