开发者

save jquery post data in a variable

I have read so many questions about this issue and I can't find an answer.

All I want to do (which I thought would be the simplest thing ever in 2011) is get some info from my database with jquery and ajax and save it in a variable to later do some stuff with it. 5 hours later I'm still here. Is this impossible to do?

The typical examples for this are:

$.post("test.php", { name: "John", time: "2pm" },
   function(data) {
     alert("Data Loaded: " + data);
   });

and

$.post("test.php", { name: "John", time: "2pm" },
   function(data) {
     $("somediv").html(data);
   }); 

So that works great... if you'll just use the value now and forget about it.

Now I don't want to display the information on the page or in an alert box, I just want to save it in some variable so I can do some calculations later.

when I assign data to variables, the values are always lost. After reading a bunch of answers about this, apparently there's no way to return data because this function is async and works differently and we don't understand how it works, etc.

For example I tried this:

return data from jquery $.post

But that didn't work for me. Or I didn't understand it correctly, is this an explanation 开发者_如何学Cor an example? Am I suppose to type callback or should I replace it with something?

Some people said call another function like here:

Jquery return post data

That didn't work either, data was passed to the function, i could use it there but again if I saved it in a variable it got lost and there was no way to return the function since where do I get what the function returns?

Finally I said you know what, I'll just put the value in a hidden field and then I'll access it. That didn't work either, I can put the value in the field but when I try to access it... it's not there yet.

Should I not use $.post? Does $.ajax make a difference? How can I accomplish this? Is it really so hard to ajax-get a value from the database and save it in javascript for later? Does everybody just use it right away? Is this really this hard to achieve?


your problem is the variable scope, you need a variable that was defined outside of the "anonymous" callback function of the ajax request. Otherwise the data is lost because you can't simply "return" it.

Your code could look like:

<html>
<head>
  <script type="text/javascript">

    // global storage
    var myData;

    // this function will be called if there is a response from the ajax request
    function doSomethingWith(response) {

      // maybe perform some formatting, parsing before

      // save to global variable
      myData = response;
    }


    // some click handlers for demonstration 

    $('#start').click(function() {
      $.post("test.php", { name: "John", time: "2pm" },
        function(data) {
          // call callback
          doSomethingWith(data);
       }); 
    });

    $('#show').click(function() {
      alert("Response was: " + myData);
    });

  </script>
</head>
<body>
  <a id="start" href="#">Fire AJAX Request!</a><br/>
  <a id="show" href="#">Show Response!</a>
</body>
</html>


Set a variable outside of the scope of your post function... like so:

var returnedData = "";
$.post("test.php", { name: "John", time: "2pm" },
   function(data) {
     returnedData = data;
   }
);


It's not at all hard to achieve, you just use a synchonous request instead of an asynchonous:

var result = $.ajax("test.php", {
  async: false,
  data: { name: "John", time: "2pm" },
});

However, this is commonly discouraged, even in the jQuery documentation, as the browser is unresponsive while waiting for the result. If possible you should really use an asynchronous approach instead.


I've been searching for along time for an answer too. I only got it working by calling a function. Whatever you need that variable for, write a function, and use it in the $.post() like so:

  $.post( url, { field: value }, 
       function( data ) { 
         yourFunction(data); 
       }); 

  function yourFunction(data){
       // check against db query
       // call other functions
  }

Like I said, I'm still searching for a better solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜