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

jQuery ajax成功匿名函数作用域

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

jQuery ajax成功匿名函数作用域

那是错误的方法。AJAX中的第一个A是异步的。该函数在AJAX调用返回之前返回(或者至少可以返回)。因此,这不是范围问题。这是订购的问题。只有两个选项:

  1. 使用选项使AJAX调用同步( 不推荐
    async: false
    ;要么
  2. 改变思维方式。代替从函数返回HTML,您需要传递一个回调,以在AJAX调用成功时被调用。

作为(2)的示例:

function findPrice(productId, storeId, callback) {    jQuery.ajax({        url: "/includes/unit.jsp?" + params,        cache: false,        dataType: "html",        success: function(html) { // Invoke the callback function, passing the html callback(productId, storeId, html);        }    });    // Let the program continue while the html is fetched asynchronously}function receivePrice(productId, storeId, html) {    // Now do something with the returned html    alert("Product " + productId + " for storeId " + storeId + " received HTML " + html);}findPrice(23, 334, receivePrice);


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

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

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