栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JQuery ajax调用的返回值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JQuery ajax调用的返回值

不幸的是,您不能将值返回给包装异步回调的函数。取而代之的是,您从AJAX请求进行的成功回调会将数据和控制权移交给另一个函数。我在下面演示了这个概念:

myFunction的定义:

// I added a second parameter called "callback", which takes a function // as a first class objectfunction myFunction(data, callback) {    var result = false;    $.ajax({        type: "POST",        contentType: "application/json",        dataType: "json",        url: "url",        data: data,        error: function(data){ result = false; // invoke the callback function here if(callback != null)  {     callback(result); } // this would return to the error handler, which does nothing //return false;        },        success: function(data){ result = true; // invoke your callback function here if(callback != null) {     callback(result);      } // this would actually return to the success handler, which does   // nothing as it doesn't assign the value to anything // return true;        }     });     // return result; // result would be false here still}

回调函数定义:

// this is the definition for the function that takes the data from your // AJAX success handlerfunction processData(result) {    // do stuff with the result here}

调用myFunction:

var data = { key: "value" }; // pass in both the data as well as the processData function object // in Javascript, functions can be passed into parameters as arguments!myFunction(data, processData);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387554.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号