jQuery variables and $.post
I'm having trouble with some code. I'm using console.log()
to output the variables contents but it seems like $.post()
is taking to long thus my variable isn't being set properly. Here is my code.开发者_JAVA技巧
$.tmsmm = {
duplicate_meeting : "something"
};
$.post("scripts/check_duplicate_meetings.php", { meeting_date: meeting_date, meeting_type: meeting_type }, function(data) {
$.tmsmm.duplicate_meeting = data;
console.log("myVar: " + $.tmsmm.duplicate_meeting);
});
console.log("myVar2: " + $.tmsmm.duplicate_meeting);
What's outputted:
myVar2: something
myVar: this is ajax data!
As you can see, myVar2 is being logged before myVar. How do I get around this?
UPDATE: The reason this solutions suited my situation best vs calling a function upon success or completion using $.post is that i have a lot of code that comes after this sample that is dependent upon the variables value. I only logged the value 2 different times to troubleshoot why my variable wasnt set outside of the $.post function. Thank you all for your help!
$.tmsmm = {
duplicate_meeting : "something"
};
$.post("scripts/check_duplicate_meetings.php", { meeting_date: meeting_date, meeting_type: meeting_type }, function(data) {
$.tmsmm.duplicate_meeting = data;
console.log("myVar: " + $.tmsmm.duplicate_meeting);
console.log("myVar2: " + $.tmsmm.duplicate_meeting);
});
Put the myVar2 in the $.post callback function to display it after myVar.
The post request happens asynchronously. So after you issue the post
request, execution immediately jumps to the console.log
at the bottom. You need to handle any response in a callback.
Put your myvar 2 code within a success function for $.post. That way you ensure its only run if $.post was successful.
You can see an example here: http://api.jquery.com/jQuery.post/
$.post
is asynchronous. The callback will run whenever the server returns something, and not before. The console.log
is directly after it, and will run synchronously, before the server has returned a response.
You can either move your console.log
code into the callback, or use $.ajax
and set the async
option to false
. The latter option is crazy and you should basically never do it, because the browser will freeze (completely!) until your server has responded. See: http://api.jquery.com/jQuery.ajax .
Try
var checkDuplicateMeetings = $.post("scripts/check_duplicate_meetings.php", { meeting_date: meeting_date, meeting_type: meeting_type }, function(data) {
$.tmsmm.duplicate_meeting = data;
console.log("myVar: " + $.tmsmm.duplicate_meeting);
});
checkDuplicateMeetings.complete(function(){
console.log("myVar2: " + $.tmsmm.duplicate_meeting);
});
For context, I am demonstrating it here with the complete
event. However, you can use success
or error
events as you see fit. Listening for a complete
event will ensure your code will be the last bit to run after the request has been made successfully or with errors. If you want to check and run a specific code for success or fail, listen for a success
or error
event accordingly.
You can force the ajax call to be synchronous by using $.ajax and using the async
property. However, this can cause the browser to hang and should be avoided.
http://api.jquery.com/jQuery.ajax/
However, that defeats the purpose of using ajax in the first place. Your example doesn't make a whole lot of sense. Why exactly do you need to check duplicate_meeting
before you get a return?
Your second call to console.log() is going to complete first every time. The first call will happen upon successful completion of the ajax call and the execution context will happen so quickly that the second call will happen first.
Try changing check_duplicate_meetings.php to just have
<?php echo 'hi'; ?>
for instance,
Then you can see if it is an error in your PHP file. The variable 'data' won't get loaded I think if the php file has an error occurring.
If it does have an error, try running check_duplicate_meetings.php
and set the variables manually in the beginning of the file to test it.
精彩评论