从 ajax 调用返回一个值到父函数

2023-09-04前端开发问题
2

本文介绍了从 ajax 调用返回一个值到父函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个函数,我需要返回一个通过 ajax 调用获得的 url.

I have a function where I need to return a url that I am getting via an ajax call.

var heatmap = new google.maps.ImageMapType({
    getTileUrl: function(coord, zoom) {
        var tileURL;
        $.get('getimage.php', { zoom: zoom, x: coord.x, y: coord.y }, function(data) {
            if(data.status) { tileURL=data.image; }
        }, "json");
        return "tileURL";
    },
       tileSize: new google.maps.Size(256, 256),
       opacity:0.55,
       isPng: true
});

显然,ajax 调用是异步的,所以我理解为什么上面的代码会返回未定义的 tileURL.我知道一般人们使用回调函数来解决这个问题,但我不知道如何让回调返回父函数的值.我正在使用谷歌地图 API,所以我真的没有任何灵活性来改变它的工作方式.

Obviously, the ajax call is asynchronous so I understand why the above code will return tileURL as undefined. I know in general people use callback functions to solve this issue, but I don't know how to get a call back to RETURN a value to the parent function. I'm working with the google maps API, so I don't really have any flexibility to change how that works.

推荐答案

因为 Ajax 请求是异步的,所以你唯一的选择就是使用回调.如果您可以向父"(实际上是调用")函数返回一个值,那么请求就不会是异步的;它会阻止调用函数.此外,无法从该函数的闭包中获取对调用函数的引用.(即,您不能 return 到调用堆栈中更高的函数).

Because the Ajax request is asynchronous, your only option is to use a callback. If you could return a value to the "parent" (really, "calling") function, then the request wouldn't be asynchronous; it would block the calling function. Also, there is no way to get a reference to the calling function from within a closure in that function. (i.e., you can't return to a function higher up in the call stack).

这篇关于从 ajax 调用返回一个值到父函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125