assigning results of $.post to a variable
|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
assigning results of $.post to a variableHi,
I'm trying to put the result of a $.post request into a variable. I've tried a few different ways, and none seem to work. I've tried working with the xmlhttprequest object that $.post returns: function mytest() { var mydate = new XMLHttpRequest(); mydate = $.post("/calendar/calendarajax.php", {mycommand: "geteventdates", month: ym}, function(data){ assigndays(data); }, "text"); alert("mydate is " + mydate.responseText); //all that assigndays does is alert(data) just to make sure I have real data. } If I just do alert(mydate) the alert box does at least show that mydate is an XMLHttpRequest object, I just can't seem to get its responseText. I've tried: function mytest() { var mydate; $.post("/calendar/calendarajax.php", {mycommand: "geteventdates", month: ym}, function(data){ mydate = data; }, "text"); alert("mydate is " + mydate); } and <script> var mydate; function mytest() { $.post("/calendar/calendarajax.php", {mycommand: "geteventdates", month: ym}, function(data){ mydate = data; }, "text"); alert("mydate is " + mydate); } </script> In theory, because I've scoped mydate using var, the mydate inside the function should refer to the mydate that was locally scoped to mytest or it should use the global variable in the second example. Any ideas on where I'm messing up? Thanks, Bob -- You received this message because you are subscribed to the Google Groups "jQuery UI" group. To post to this group, send email to jquery-ui@.... To unsubscribe from this group, send email to jquery-ui+unsubscribe@.... For more options, visit this group at http://groups.google.com/group/jquery-ui?hl=en. |
|
|
Re: assigning results of $.post to a variableHi Bob,
The problem here is synchronicity. The post fires, but the mainline javascript isn't waiting for the $.post to return before continuing. THEREFORE, if you want sonething to happen post-$.post, use a callback. **--** Steve On Nov 9, 12:42 pm, Bob Ramsey <alfrod...@...> wrote: > Hi, > > I'm trying to put the result of a $.post request into a variable. > I've tried a few different ways, and none seem to work. I've tried > working with the xmlhttprequest object that $.post returns: > > function mytest() > { > var mydate = new XMLHttpRequest(); > mydate = $.post("/calendar/calendarajax.php", {mycommand: > "geteventdates", month: ym}, function(data){ > assigndays(data); > }, "text"); > alert("mydate is " + mydate.responseText); > //all that assigndays does is alert(data) just to make sure I have real data.} > > If I just do alert(mydate) the alert box does at least show that > mydate is an XMLHttpRequest object, I just can't seem to get its > responseText. > I've tried: > function mytest() > { > var mydate; > $.post("/calendar/calendarajax.php", {mycommand: "geteventdates", > month: ym}, function(data){ > mydate = data; > }, "text"); > alert("mydate is " + mydate); > > } > > and > <script> > var mydate; > function mytest() > { > > $.post("/calendar/calendarajax.php", {mycommand: "geteventdates", > month: ym}, function(data){ > mydate = data; > }, "text"); > alert("mydate is " + mydate);} > > </script> > > In theory, because I've scoped mydate using var, the mydate inside the > function should refer to the mydate that was locally scoped to mytest > or it should use the global variable in the second example. > > Any ideas on where I'm messing up? > > Thanks, > > Bob -- You received this message because you are subscribed to the Google Groups "jQuery UI" group. To post to this group, send email to jquery-ui@.... To unsubscribe from this group, send email to jquery-ui+unsubscribe@.... For more options, visit this group at http://groups.google.com/group/jquery-ui?hl=en. |
|
|
Re: assigning results of $.post to a variableOn Nov 9, 12:42 pm, Bob Ramsey <alfrod...@...> wrote:
> Hi, > > I'm trying to put the result of a $.post request into a variable. > I've tried a few different ways, and none seem to work. I've tried > working with the xmlhttprequest object that $.post returns: > > function mytest() > { > var mydate = new XMLHttpRequest(); Why are you instanciating an XMLHttpRequest ?? > mydate = $.post("/calendar/calendarajax.php", {mycommand: You are discarding it right at the next line !! Moreover, XMLHttpRequest is not a valid Javascript object in some browsers (see http://en.wikipedia.org/wiki/XMLHttpRequest). But in any case, JQuery does it for you so you don't have to take care of that. > "geteventdates", month: ym}, function(data){ > assigndays(data); > }, "text"); > alert("mydate is " + mydate.responseText); > //all that assigndays does is alert(data) just to make sure I have real data.} > > If I just do alert(mydate) the alert box does at least show that > mydate is an XMLHttpRequest object, I just can't seem to get its > responseText. > I've tried: > function mytest() > { > var mydate; > $.post("/calendar/calendarajax.php", {mycommand: "geteventdates", > month: ym}, function(data){ > mydate = data; > }, "text"); > alert("mydate is " + mydate); > > } > > and > <script> > var mydate; > function mytest() > { > > $.post("/calendar/calendarajax.php", {mycommand: "geteventdates", > month: ym}, function(data){ > mydate = data; > }, "text"); > alert("mydate is " + mydate);} > > </script> > Yes, but the alert prints out "mydate" before the request has even been made! The order of things happening in this script is : 1.initiate a POST request to given url and with given params 2. alert "mydate is "+ mydate (will output "mydate is undefined" since never assigned) 3. send request (most browser's javascript engine are single threaded, so you couldn't send the request while still being executing something) [... after a few milliseconds...] 4. receiving a response 5. assigning it to mydate Remember that, when playing with Ajax, generally, everything is or should be asynchronous, meaning that the order of execution do or should not happen in a procedural way. function mytest() { $.post("/calendar/calendarajax.php", {mycommand: "geteventdates", month: ym}, function(data){ var mydate = data; alert("mydate is " + mydate); // or set a DOMElement's text : $('selector').text("mydate is " + mydate); // is a better alternative to debug things than an alert box... }, "text"); } It is not good practive to declare "var mydate;" globally or outside a function scope. If you need the mydate variable somewhere else in your code, you should, rather, store it somewhere else. Read jQuery.data (http://docs.jquery.com/Internals/jQuery.data) to store raw data (used usually for storing flags and element's states). Hope this helps. -- You received this message because you are subscribed to the Google Groups "jQuery UI" group. To post to this group, send email to jquery-ui@.... To unsubscribe from this group, send email to jquery-ui+unsubscribe@.... For more options, visit this group at http://groups.google.com/group/jquery-ui?hl=. |
| Free embeddable forum powered by Nabble | Forum Help |