调用ajax返回函数外部?(Calling ajax returned outside the function? [duplicate])

编程入门 行业动态 更新时间:2024-10-17 17:25:02
调用ajax返回函数外部?(Calling ajax returned outside the function? [duplicate])

这个问题在这里已有答案:

如何从异步调用返回响应? 32个答案 jQuery:ajax调用成功后返回数据[重复] 5个答案

我试图调用在其函数之外返回的ajax,下面是我尝试过的代码但是没有用。

var names; $.ajax({ type: 'POST', url: 'ajax/get_upcoming.php' }).success(function (data) { names = data[1]; }); alert(names);

This question already has an answer here:

How do I return the response from an asynchronous call? 35 answers jQuery: Return data after ajax call success [duplicate] 5 answers

I am trying to call the ajax returned outside its function, below is the code I tried but didn't work.

var names; $.ajax({ type: 'POST', url: 'ajax/get_upcoming.php' }).success(function (data) { names = data[1]; }); alert(names);

最满意答案

因为成功函数是回调函数,并在ajax请求获得响应时调用,所以改为尝试这个

var names; $.ajax({ type: 'POST', url: 'ajax/get_upcoming.php', success: performOperation }); function performOperation(data) { // write your code here names = data[1]; }

Because success function is callback function and called when ajax request get the response , so instead try this

var names; $.ajax({ type: 'POST', url: 'ajax/get_upcoming.php', success: performOperation }); function performOperation(data) { // write your code here names = data[1]; }

更多推荐

本文发布于:2023-08-06 22:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Calling   ajax   duplicate   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!