jquery返回发布数据

编程入门 行业动态 更新时间:2024-10-27 08:35:03
本文介绍了jquery返回发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

function getAreas(){ $ .post(/ custom_html /weathermap.php,',函数(数据){ 返回数据 },json); }

这很好。现在我想要做的是将数据传递回变量,换句话说: pre $ var $ data = getAreas( )

但它不返回任何内容。有没有办法让这个工作?

Thanx事先提供任何帮助。 / b>这是一个异步调用,所以你不能像这样返回。

你必须将代码与 data 放入回调函数( function(data){} )。

function getAreas(){ $ .post(/ custom_html / weathermap.php,'', function(data){ //在这里用数据做一些事情,比如调用另一个函数 },json); }

需要一段时间让自己的头脑进入异步思维方式,但你会解决它。基本上,一旦请求被发送,发送请求的代码就完成了。该线程的执行将完成,并且您的浏览器将只是坐在那里,不做任何事情。那么 $。post 调用将从 weathermap.php 返回数据,并且您的回调函数将被调用。这开始了一个全新的执行线程。尝试将它们看作两个完全独立的执行,一个是pre-ajax调用和一个post-ajax调用。

这里有一些ascii的优点:

V | 用户点击按钮(或其他事情发生) | | 您的JavaScript运行 | | 并最终获得给ajax调用-------> SERVER ------>服务器发回数据 | | 并且您的回调在数据上执行并执行从这里继续 | | V

Ok so I have this function:

function getAreas(){ $.post("/custom_html/weathermap.php",'', function(data){ return data }, "json"); }

which works fine. now what I am trying to do is to pass the data back to a variable, in other words:

var data=getAreas()

but it doesnt return anything. is there a way to get this working?

Thanx in advance for any help.

解决方案

It's an asynchronous call, so you can't return from it like that.

You'll have to move the code that does something with data into the callback function (function(data){}).

function getAreas(){ $.post("/custom_html/weathermap.php",'', function(data){ //do something with data here, such as calling another function }, "json"); }

It takes a while to get your head into the asynchronous way of thinking, but you'll work it out. Basically, the bit of code that sends the request is done once the request is sent. That thread of execution will finish, and your browser will just sit there, not doing anything. then the $.post call will get the data back from weathermap.php, and your callback function will be called. That starts a whole new thread of execution. Try to think of them as two completely separate executions, one pre-ajax call and one post-ajax call.

Here's some ascii goodness:

V | User clicks button (or something else happens) | | Your JavaScript runs | | And eventually gets to the ajax call -------> SERVER ------> Server sends data back | | And your callback is run on the data and execution continues from here | | V

更多推荐

jquery返回发布数据

本文发布于:2023-10-12 00:21:54,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   jquery

发布评论

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

>www.elefans.com

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