js回调函数在项目中的运用
来源:互联网 发布:windows ce应用小游戏 编辑:程序博客网 时间:2024/06/16 13:18
js回调函数用处非常多,现在我就简单的介绍点基础的
代码:
var senderId = $.cookie("senderId");var secToken = $.cookie("secToken");var userName = $.cookie("userName");var urlPath = "../../develop/";var popu;var popu2;var seme = [];//写一个绑定课程的方法var registerCourse = { getCurrentSemester:function(jsondata){ var action = "Lab_getCurrentSemester"; return ajaxdata(action,jsondata); }, getCourse:function(jsondata){ var action = "Lab_getLabCourseList"; return ajaxdata(action,jsondata); } }function ajaxdata(action,jsondata){ var returnvalue ; var callback = function(func,msg){ return func(msg); } var func = function(msg){ console.log("回调函数执行了"); console.log(msg); returnvalue = msg; console.log(returnvalue); //获取学期的判断 if(msg.localSemester !=null){ returnvalue = msg.localSemester; } //课程的数据 if(msg.localCourselist !=null){ returnvalue = msg.localCourselist; } return returnvalue; } $.ajax({ async:false, dataType:"json", type:"post", url:urlPath+action, data:jsondata, success:function(msg){ console.log(msg); console.log("success"); console.log(func+msg.localSemester) callback(func,msg); }, error:function(){ alert("请求失败"); } }) console.log(returnvalue); return returnvalue;}//alert(registerCourse.getCurrentSemester(jsondata));$(function() { //注册课程的操作 $("#registercourse").click(function(){ $(".xiaochu").remove(); var year = $("#year").val(); var semester = $("#semester").val(); console.log(year+semester); //changesemester(semester); $("#yearin").val(year); console.log(changesemester(semester)); $("#semesterin").val(changesemester(semester)); var jsondata ={ senderId:senderId, secToken:secToken, semester:semester, year:year, } var semester = registerCourse.getCurrentSemester(jsondata); var course = registerCourse.getCourse(jsondata); console.log("获取结果"); console.log(semester); console.log(course); $.each(course ,function(index,row){ console.log(row); if(row.localName != null){ $("#coursecodein").append("<option class='xiaochu' value="+row.localCourseCode+">"+row.localName+"</option>"); } }) $("#mymodal").modal("toggle"); }); )}
我们在webservice的项目或者其他项目中经常出现这样的操作,ajax请求数据,服务器的得到相应给数据,处理数据,页面上的action调用数据,这段代码的思想就是封装一个函数类,action时候调用类,给变量调用类的方法,将共有的部分封装ajaxdata函数里面回调函数获取ajaxsuccess的数据,并处理。
阅读全文
0 0
- js回调函数在项目中的运用
- 关于回调函数在Java中的运用(1)
- js中回调函数的运用
- JS中的回调函数
- CORBA回调函数在C#项目环境中的应用
- js和jquery中的回调函数
- 理解 JS 回调函数中的 this
- 回调函数的运用
- 回调函数在MFC中的使用
- 回调函数在android中的使用
- js回调函数
- js回调函数
- JS回调函数
- js回调函数
- js回调函数
- js回调函数
- js回调函数
- js-回调函数
- Hibernate 常见异常
- Linux系统结构 详解
- javascript常用实例的实现与封装1
- java.sql.SQLException: Generated keys not requested. You need to ... 错误
- mybatis中 插入字符串空值无法修改
- js回调函数在项目中的运用
- python用dlib关键点将人脸背景区域去除
- 顺序表的创建,插入,修改,删除,查询代码
- ArcGIS JS 实现前端添加shape并保存到服务器
- pc端防止百度转码
- 7-表格显示案例
- 代码检测工具 joern 安装
- spring cloud 入门实践系列
- java设计模式之---备忘录模式