It is now more popular to use jquery's ajax to achieve some non-refresh request effects. This chapter provides a very simple code example for your reference. I hope it can bring some help to friends in need. The code is as follows:
<script type="text/javascript"> /* 请求Ajax 带返回值,并弹出提示框提醒 --------------------------------------------------*/ function getAjax(url,parm,callBack) { $.ajax({ type:'post', dataType:"text", url:url, data:parm, cache:false, async:false, success:function (msg) { callBack(msg); } }) } /*删除 /*url: 表示请求路径 --------------------------------------------------*/ function DeleteData(url,id) { var parm='active=Del&id='+id; if(id==undefined||id=="") { showAlertMsg('请选择要删除一行'); } else { showConfirmMsg("此操作不可恢复,确定要删除吗?",function(r){ if(r) { getAjax(url,parm,function(rs) { if(parseInt(rs)>0) { showOverAlertMsg("删除成功!",2000,4); } else if(parseInt(rs) == 0) { showOverAlertMsg("该数据被关联,无法删除!",2000,3); } else { showOverAlertMsg("删除失败!", 2000, 5); } }) } }) } } //删除 function DeleteOnclick() { DeleteData('SysMenu_List.aspx', Menu_Id); } </script>
async is the abbreviation of asynchronous [asynchronous], it is a bool value that defaults to true. When async is true, the ajax request will be executed regardless of whether it is completed. Synchronous requests temporarily lock the browser and do not perform any action while the request is being executed.
Describe the functions to be implemented in the past two days. Determine whether a data exists in the database. If it exists, an error will be returned. If it does not exist, it can be filled in and submitted. The code is as follows:
isCompany :function(name){ var flag = 0; if(name == '') { return false; }else{ $.ajax({ type: "POST", url: '/checkCompany/name/' + name, cache: false, success: function(data){ return data > 0 ? false : true; } }) } }
Through the above code, it has been unable to correctly reflect whether the name already exists in the database. By setting a global variable and changing async (the default is true) from asynchronous to synchronous, the return value of ajax can be successfully obtained. The code is as follows
isCompany :function(name){ var flag = 0; if(name == '') { return false; }else{ $.ajax({ type: "POST", url: '/checkName/name/' + name, cache: false, async: false, success: function(data){ flag = data; } }) } return flag > 0 ? false : true; }
The above is the content of using ajax in jquery to submit data and then the website backend will return the data based on the data we submitted. I hope it will be helpful to everyone learning ajax.