jquery返回发布数据 [英] Jquery return post data

查看:152
本文介绍了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返回发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆