Shadowing Posted June 16, 2012 Share Posted June 16, 2012 Hey guys this is driving me crazy i dont know why i cant simply return a value like i would in a php functionI can never read out side ajax brackets, the script isnt failing eitherrank alerts "0" on success says rank is undefined function grab_alliance_rank() { $.ajax({ url: "ajax/politics.php?functions=rank", type: 'POST', dataType: 'json', error: function(){ alert("Oops... seems to be a problem retrieving data. function alliance rank") }, success: function(response) { var rank = (response.rank); alert(rank); // this works here } });return rank ; } Link to comment Share on other sites More sharing options...
dsonesuk Posted June 16, 2012 Share Posted June 16, 2012 scope is not the problem here, when the ajax function is called it goes away to do its thing, but the' return rank;' kicks off before response is returned. The use of async: false, fixes this, because it forces the script to pause and wait for a response, then continue and so the return has a value to return. Problem: async: false is deprecated, and you are advised not to use this as i said it causes delay while it waits for returned response, this sometimes causes a locking of the page especially for older browsers. Solution: use callback function to retrieve response example function grab_alliance_rank(rank) { $.ajax({ url: "ajax/politics.php?functions=rank", type: 'POST', dataType: 'json', error: function(){ alert("Oops... seems to be a problem retrieving data. function alliance rank") }, success: function(response) { rank(response.rank); } });} <div id="puthere"></div><script>grab_alliance_rank(function(getrank){document.getElementById("puthere").innerHTML=getrank;})</script> Link to comment Share on other sites More sharing options...
Shadowing Posted June 18, 2012 Author Share Posted June 18, 2012 Thanks for the reply as always dsonesuk im a little confused lol this below is how im grabing the rank grab_alliance_rank(function(getrank){document.getElementById("puthere").innerHTML=getrank;}) how am I storing the rank? dont i have to put something here to store it success: function(response) { var rank = (response.rank); // store rank some how here } and dont I need a return on grab_alliance_rank function function grab_alliance_rank(rank) { $.ajax({ // grabs the users alliance rank url: "ajax/politics.php?functions=rank", type: 'POST', dataType: 'json', error: function(){ alert("Oops... seems to be a problem retrieving data. function alliance rank") }, success: function(response) { var rank = (response.rank); // store rank some how here } }); return rank;} so I guess this some how inserts the agrument?document.getelementbyid works as a print or echo sort of speak? function(getrank){document.getElementById("puthere").innerHTML=getrank;} Link to comment Share on other sites More sharing options...
dsonesuk Posted June 18, 2012 Share Posted June 18, 2012 Look at how function grab_alliance_rank sends another function as a argument as you would in normal circumstance when you would use function rank(getrank){document.getElementById("puthere").innerHTML=getrank;} with a call of function rank("whatever rank") you end up with "whatever rank" displayed in element with id "puthere" You are basically doing the same where 'rank' in 'grab_alliance_rank(rank)' becomes the reference to function you sent as argument that you will call to send the returned response back wiith, where 'getrank' in function(getrank) will be the value of "whatever rank".function grab_alliance_rank(rank) { $.ajax({ url: "ajax/politics.php?functions=rank", type: 'POST', dataType: 'json', error: function(){ alert("Oops... seems to be a problem retrieving data. function alliance rank") }, success: function(response) { rank(response.rank); } });}<div id="puthere"></div><script>grab_alliance_rank(function(getrank){document.getElementById("puthere").innerHTML=getrank;}) Link to comment Share on other sites More sharing options...
Shadowing Posted June 18, 2012 Author Share Posted June 18, 2012 omg dsonesuk lol I think i have to give you more code in how im using it cause im still clueless, then you can see my error and give me a jab in the face. function politics() { // trying to read the rank in this function rank = grab_alliance_rank( function(getrank) { document.getElementById("alliance_rank").innerHTML=getrank; } )alert(rank); } function grab_alliance_rank(rank) { $.ajax({ // grabs the users alliance rank url: "ajax/politics.php?functions=rank", type: 'POST', dataType: 'json', error: function(){ alert("Oops... seems to be a problem retrieving data. function alliance rank") }, success: function(response) { var rank = (response.rank); } }); } <div id="alliance_rank"></div> Link to comment Share on other sites More sharing options...
dsonesuk Posted June 18, 2012 Share Posted June 18, 2012 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script><script type="text/javascript">/*<![CDATA[*//*---->*/function grab_alliance_rank(rank) { $.ajax({ url: "politics.php?functions=rank", type: 'POST', dataType: 'json', error: function(){ alert("Oops... seems to be a problem retrieving data. function alliance rank") }, success: function(response) { rank(response.rank); } });}/*--*//*]]>*/</script><style type="text/css"></style></head><body><div id="alliance_rank"></div><script>function politics() { // trying to read the rank in this function grab_alliance_rank( function(rank) { document.getElementById("alliance_rank").innerHTML=rank; alert(rank); }) }politics()</script></body></html> Link to comment Share on other sites More sharing options...
Shadowing Posted June 19, 2012 Author Share Posted June 19, 2012 when i call politics(); alert doesnt even pop up Link to comment Share on other sites More sharing options...
dsonesuk Posted June 19, 2012 Share Posted June 19, 2012 I forget, I have this magical pc which means any code i run in any browser works! and you poor minions have to deal with your poor pathetic substandard O/S, OR you have some conflict in the code, or you are doing it in such away that it is failing to achieve its intended end result. and no i'm not going to dump my pc in the hot volcanic pool of mount doom. Link to comment Share on other sites More sharing options...
Shadowing Posted June 22, 2012 Author Share Posted June 22, 2012 lol dosonesuk. well it would explain alot and all this time i thought you was a coding god when it is really just your OS if I do this alert doesnt even pop up $(function(){ politics(); function politics() { grab_alliance_rank( function(rank) { document.getElementById("alliance_rank").innerHTML=rank; alert("test"); // doesn't pop up }); alert("test"); // does pop up} }); Link to comment Share on other sites More sharing options...
Ingolme Posted June 22, 2012 Share Posted June 22, 2012 Check the error console. In Firefox you can see it by pressing Control+Shift+J. It probably says "document.getElementById("alliance_rank") is null" Link to comment Share on other sites More sharing options...
dsonesuk Posted June 22, 2012 Share Posted June 22, 2012 My magical browsers show both alerts, BUT! if you change both to alert(rank), only the first will work as rank is only defined in function(rank). Link to comment Share on other sites More sharing options...
Shadowing Posted June 22, 2012 Author Share Posted June 22, 2012 no errors in firebugi clicked on html and looked at the div and it was blank. but idk would i see it there? still though alert("test") doesnt work so activating function grab_alliance_rank never trips the alert Link to comment Share on other sites More sharing options...
Shadowing Posted June 22, 2012 Author Share Posted June 22, 2012 alright 'll put all this on a seperate page to test it out hehe Link to comment Share on other sites More sharing options...
Shadowing Posted June 22, 2012 Author Share Posted June 22, 2012 i put this below on its own page. and alert doesnt pop up, no firebug errors either $(function(){ politics(); function politics() { grab_alliance_rank( function() { alert("test"); // doesn't pop up }); } function grab_alliance_rank() {}}); Link to comment Share on other sites More sharing options...
justsomeguy Posted June 22, 2012 Share Posted June 22, 2012 The function with the alert never gets executed. You pass that function as a parameter to grab_alliance_rank, which does exactly nothing at all. Link to comment Share on other sites More sharing options...
dsonesuk Posted June 22, 2012 Share Posted June 22, 2012 $(function(){function grab_alliance_rank(ThisArgumentMustExist) { // this argument will be used as name ref to function passed as argumentvar thisCommamd = "grunt";ThisArgumentMustExist(thisCommamd);}function politics() { grab_alliance_rank( //the function passed as argument start function(ThisArgumentMustAlsoExist) { alert(ThisArgumentMustAlsoExist); // doesn't pop up } //the function passed as argument end); } politics(); }); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.