Ajax :: Variable Is Undefined Outside Of Callback Function?
Mar 4, 2009
I have been trying to figure this out for a few days now and have finally come to the point where I think I got it but may just need a little push to get what I want.
Heres the situation:
I have this function that uses ajax to access a database and returns the results in the responseText. That part is working fine, I have confirmed that the responseText contains the information that I need
**************code*****************************************
//new ajax already created and assigned to the variable xmlhttp//
var response_recieved = "some data";
function get_answer(Table_Name, Field_Name, Data_Type) {
[Code]....
As evidence by my posting, this is not working correctly. The variable response_recieved is coming up undefined outside of the callback function assigned to the onreadystatechangeproperty.
Based on the information that I have gathered online from various forums, I believe this issue has to deal with a problem with closure in the callback function assigned to onreadystatechange property. I don't think it has any. Based on the fact that there is no anonymous function in that function. But my problem is that the call back function is anonymous itself... I think???
get the variable response_recieved to survive outside of this function.
What I did wrong to make this code not work. Can you put a variable in a function callback? <script type="text/javascript"> var aaa = prompt('number?') var obja = new Const(aaa); functin Const(numb){ this.x=numb; alert(obja.x); } </script>
This is the code. I tried exploring the options of SQL for solving this, but they are limited. I want to pass newSync[i].id to the onsuccess callback function, but I fail
Code:
I fail even if I add newSync[i].id as a third argument to function(tx,results)
I am using jQuery 1.4.2 and I am trying to carry over the global variable totaltabs into my JSON callback function. The code seems to work properly, however, in my loadJSON function after the callback function loads the data, my totaltabs variable will not increment.
Here is a peak at my code.
totalItems is loaded from another function, not relevant to my example.
Code JavaScript: var totaltabs = 0; $(document).ready(function(){ resize();
I can't seem to get a variable declared from within a post callback, so that I could manipulate my script accordingly. I have delete.php, that deletes an id from a mysql db. If it fails, I want the script to stop and display the error. The .php script echos an "ok" if everything went fine, if not - it echos the error.var abort_error; // set the error variable
I'm just starting out with Javascript as a development language and this will probably be a relatively simple problem for someone to solve for me.
I am trying to access a variable (this.bodyEl.innerHTML) from within a function but get an error message indicating that it is "undefined". I know that it is a valid variable because I call it elsewhere outside of the inner function itself.
I'm sure this is just a scope issue, but I'd welcome any suggestions on how to solve it with an explanation of where I've gone wrong if you have the time.
[code] Hide from browsers that do not understand Javascript.The addLoadEvent function adds functions to the window.onload command to load multiple functions at startup function addLoadEvent(func)[code]When the page loads I get the nice alert box that says loading and then one that says count equals 0.When i click my edit list drop down and choose modify I get a message box that reads count equals in edit function 0.If I type text in the textbox and leave the text box I get an alert box that reads count equals in subcheck function Undefined and then another one that says NaN.
I've written a function to format a number. It strips dollar signs and commas, converts to a number, and sets the number to two decimal places.However, I get the error message "amountNum is undefined". It occurs right where I use the replace command.[code]I am calling the function with this line of code:[code]
I am trying to retrieve json data from the world bank's API, here's my code: <html> <head> <script src="[URL]"></script> <script type="text/javascript"> var theData; $(document).ready(function(){ $("button").click(function(){ $.ajax({ url: '[URL]', success: function(data) { /*this alert message does not show*/ alert(data); }, dataType: 'json' }); }); });
</script> </head> <body> <p></p> <button> Get World Bank Data </button> </body> The callback function is not executed but there is response when I checked it using firebug.
I have Javascript code that looks like this: var data1; $.post('save_search.php', formData, function(data) { data1 = data; }); jsonData = eval('(' + data1 + ')'); if (jsonData.return_status.search("successful") > -1) $('#msg_div').html("<font color=red>Search was saved</font>"); else $('#msg_div').html("<font color=red>Search was not saved. Try saving again.</font>"); "data1" comes up as undefined in the statement jsonData = eval('(' + data1 + ')');
Even though "data" is a perfectly correct JSON string *inside* the callback function! I can put the eval statement inside the callback function and it will form a good JSON object, like this: jsonData = eval('(' + data + ')'); I'm simply trying to get my Ajax response data to the outside of my callback function so I can use it in other Javascript code. I've never seen an ordinary function behave this way.
On a project I got stuck on this "problem". When you make a json (ajax) call. You also need a callback function. Which means I have to call the next $.getJSON within that callback function to read the next portion of data. In my opinion this is not very readable code...What I want is a nice and readable piece of code. Where you could just write it like this: var data1 = $.getJSON(..); var data2 =$.getJSON(..); I've found a solution here: [URL] (explains my problem perfectly). But it's not compatible with the getJSON story. Is there already a possibility to do this with the jquery library?
I have a really simple scenario which got broken after upgrading to 1.5.the defined handler for the success callback in $.ajax doen't set the data parameter - it's always undefined. This works fine in 1.4.4.
Theres nothgin wrong with my ajax, it works and is sending other variables but whe i want to send "pood" from function farmers(pood, happy) i twill not work. now when i call the function it goes like this code...
now it will not send PISSLET, what do i do? its making me angry this is the querystring the other variables are sending to my ajaz page and i echo them out, but thie pood variable will not work . i even tried redefining it code...
I'm trying to fetch JSON contents via ajax() from a file using this code: // grab pages via AJAX request sm.pages = (function() { var json = null; $.ajax({ async: false, global: false, url: 'pages.json', dataType: 'json', success: function(data){ json = data; console.log(data); }}); return json; })(); One problem though. The success function doesn't seem to be firing at all even though I can see the request was successfully in Firebug.
I am attempting to use the return value from a nested ajax function as the value for a variable in its parent. However, despite being able to successfully assign the variable within the nested function, it reverts back to its original value after the child function has terminated. Below is the code:
I am trying to convert some of my javascripts into a class and am running into difficulties which i think are related to variable scope.
Basically I have a constructor function for a calendarInput class that takes 4 parameters, the first is a reference name/number for this input. I also have some functions for importing my PHP classes into Javascript using AJAX (properties only. still trying to get methods working but that's another story!). The relevant function is called call_object_method(classname, methodname, createparams, methodparams, post, callbackfunction). This creates an instance of the specified PHP class using the parameters in createparams. It then calls the specified method using the parameters in methodparams. The result is passed back to the javascript function specified in the callbackfunction parameter (ie the value of xmlhttp.onreadystatechange is set to callbackfunction before xmlhttp.send() is called)
The function i am trying to fix is called show (x,y) which creates the html for the calendarInput and displays it at co-ordinates x, y.
this.showcallback = function() { alert(this); <!--//code to create html//--> }
}
I know i've cut out most of the innards of this. This is because I have already tested these functions and had the calendarInput working outside of a class, hence am pretty sure that this is ok (plus it runs to almost 1000 lines these days!!). My problem is that when I call the show method, the alert on the first line of the callback function returns the function showcallback instead of (as i was expecting) the instance of the calendarInput object. Whilst this kinda makes sense I can't figure out how to reference the Object instead. I have tried 'this.parent' but this returned undefined. I have tried changing the way i reference the callback function (ie the final parameter of call_object_method) but no joy.
I am having some fun with javascript, and I came upon a site, on the site you have to try to figure out the passwords which are embedded in javascript, now im on the last level but i have no clue whats going. It seems to be an undefined variable. Can anyone point me in the right direction? Code:
Does anyone know why the onclick in the following popup menu gives the error:"Val is undefined"? Does it have something to do with the fact that it is called within the variable tablePop? Because it IS displayed properly as part of the popup text, where it is called outside the single quotation marks (see [***]). It is only in the onclick that it's causing problems. Code: