1. Post #1
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Hi there,

    I have the following jQuery code:
    Code:
    $(function() {
    
    	$.ajax({
    
    		url: siteCall,
    
    		success: function(data, textStatus, XMLHttpRequest) {
    
    		returnData = data;
    
    		}
    
    	});
    
    });
    What I want to do is pass the variable 'returnData' from inside jQuery so that I can use it in Javascript elsewhere on the page.
    Can someone please tell me how to do this?

    Thanks in advance!

  2. Post #2
    Gold Member
    Zaldos's Avatar
    April 2010
    355 Posts
    create it at the beginning before the JQuery code. Just on its own. Then assign data to it in the function like you have.
    e.g:
    Code:
    <script>
    returnData = data;
    
    //your code
    </script>
    Or call a function you have created to do something with the data.

  3. Post #3
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    create it at the beginning before the JQuery code. Just on its own. Then assign data to it in the function like you have.
    e.g:
    Code:
    <script>
    returnData = data;
    
    //your code
    </script>
    Or call a function you have created to do something with the data.
    I'm trying the function part in a second but this didn't work:
    Code:
    var returnData;
    
    	$(function() {
    		$.ajax({
    			url: siteCall,
    			success: function(data, textStatus, XMLHttpRequest) {
    			returnData = data;
    			}
    		});
    	});
    Are you sure it's put in the right place in the jQuery?

  4. Post #4
    Gold Member
    Zaldos's Avatar
    April 2010
    355 Posts
    try
    Code:
    returnData = "";
    not
    Code:
    var returnData;

  5. Post #5
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    try
    Code:
    returnData = "";
    not
    Code:
    var returnData;
    That didn't work either :S And I get no errors :/
    It returns the ""?

  6. Post #6
    Gold Member
    Zaldos's Avatar
    April 2010
    355 Posts
    Have a look at this.
    http://valisto.com/dev/index.php
    It seems to work for me.

  7. Post #7
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Weird!
    I am trying "sort of" the same here:
    http://127.0.0.1/stock/debug_ajax.php
    Of course what I really want to do is being able to manipulate
    the data that I receive from "jq_username.php?username=eax".
    (It either returns 0 or 1 and I want to do and if statement on that).

    Can you see what's wrong? :S

  8. Post #8
    Gold Member
    Zaldos's Avatar
    April 2010
    355 Posts
    No I cant. Its a local host. Not your external url.

    But if you want to manipulate look at my url again.

  9. Post #9
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Oh shit! Forgot to put it on my FTP SORRY! XD

    Here's the real link:
    http://stock.eax.dk/debug_ajax.php

    Sorry!

  10. Post #10
    Gold Member
    Zaldos's Avatar
    April 2010
    355 Posts
    Try the url http://stock.eax.dk/jq_username.php?username=eax

    I think it has something to do with the url directory. Or I may be mistaken.

  11. Post #11
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Hmm how so?
    They are in the same directory if that is what you mean?

  12. Post #12
    Gold Member
    Zaldos's Avatar
    April 2010
    355 Posts
    I really don't know sorry.

  13. Post #13
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    I really don't know sorry.
    Fair enough, thanks anyway! : )

    Anyone else knows? : )

  14. Post #14
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    I get cakes when I view the page. It looks like you're doing it correctly.

  15. Post #15
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    I get cakes when I view the page. It looks like you're doing it correctly.
    Cakes is what it starts with, it should replace the cakes with "1" :S

  16. Post #16
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    Cakes is what it starts with, it should replace the cakes with "1" :S
    Oh, lemme look again.

    Edited:

    Hey mate, here you go. Make sure the file is server-side.

    Then just set the variable to what you need it to be:

    http://srcsrv.com/test.php

    Edited:

    P.s:

    Click!

  17. Post #17
    Gold Member
    Darkimmortal's Avatar
    April 2009
    2,727 Posts
    You can't access a variable set in a callback until after the callback has happened.

    Hence why the [first] A in AJAX stands for Asynchronous.

  18. Post #18
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    You can't access a variable set in a callback until after the callback has happened.

    Hence why the [first] A in AJAX stands for Asynchronous.
    Unless you block. Which is dumb. Very, very dumb.
    Reply With Quote Edit / Delete Reply United States Show Events Agree Agree x 1 (list)

  19. Post #19
    a2h
    Gold Member

    September 2008
    2,460 Posts
    try
    Code:
    returnData = "";
    not
    Code:
    var returnData;
    PLEASE DO NOT DO THIS. You really should get used to declaring variables as I have had bad experiences with some browsers from not doing so, and it also ensures the habit carries across to languages that have declaring variables mandatory.

    Also, you can do this:

    Code:
    var foo = "bar";
    OH AND: :siren: jQuery is not a separate language from JavaScript, it is a set of functions and methods for use in JavaScript. :siren:

  20. Post #20
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Oh, lemme look again.

    Edited:

    Hey mate, here you go. Make sure the file is server-side.

    Then just set the variable to what you need it to be:

    http://srcsrv.com/test.php

    Edited:

    P.s:

    Click!
    Thanks a lot : )

    I have tried this, but to no avail:
    Code:
    var returnData = "cakes";
    
    			
    
    	$(document).ready(function() {
    
    		$.ajax({
    
    		  url: "jq_username.php?username=eax",
    
    		  global: false,
    
    		  type: "GET",
    
    		  dataType: "text",
    
    		  cache: false,
    
    		  success: function(returnData, successCode, requestObject){
    
    		returnData = requestObject.responseText;
    
    		//$("#field").html(returnData);
    
    		}
    
    	}
    );				
    
    });
    
    
    document.getElementById("yourmom").innerHTML = returnData;
    But it still writes out 'Cakes' :S

    Any idea what is wrong?

    PLEASE DO NOT DO THIS. You really should get used to declaring variables as I have had bad experiences with some browsers from not doing so, and it also ensures the habit carries across to languages that have declaring variables mandatory.

    Also, you can do this:

    Code:
    var foo = "bar";
    OH AND: :siren: jQuery is not a separate language from JavaScript, it is a set of functions and methods for use in JavaScript. :siren:
    Okay I won't do that! :)

    Yes I know it's a set of functions and methods :)

  21. Post #21
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    It's ALWAYS going to print Cakes. It's asynchronous for a reason. You need to update it in the AJAX success function.

  22. Post #22
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    It's ALWAYS going to print Cakes. It's asynchronous for a reason. You need to update it in the AJAX success function.
    Okay, fair enough, can you please show me how to actually do that? I think I've tried just about everything, everything that is not the correct thing >__<

    My code:
    Code:
    var returnData = "cakes";
    
    	$(document).ready(function() {
    
    		$.ajax({
    
    		url: "jq_username.php?username=eax",
    
    		global: false,
    
    		type: "GET",
    
    		dataType: "text",
    
    		cache: false,
    
    		success: function(returnData, successCode, requestObject){
    
    			returnData = requestObject.responseText;
    
    			}
    
    	}
    
    );
    });
    
    
    document.getElementById("yourmom").innerHTML = cakes;

  23. Post #23
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    I'll edit what I posted before:

    Code:
    <script id="source" language="javascript" type="text/javascript"> 
    	var returnData;
    	var userName = "eax";
    	
    	
    	$(document).ready(function() {
    		$(document).click(function(){
    			$.ajax({
    				  url: "jq_username.php?username=" + userName,
    				  global: false,
    				  type: "GET",
    				  dataType: "text",
    				  cache: false,
    				  success: function(returnData, successCode, requestObject){
    					returnData = requestObject.responseText;  	// Herp Derp this sets your return Data
    					 $("#field").html(returnData);				// This sets the HTML to to what it returned
    				  }
    			   }
    			);
    		});
    	});
    </script>
    Next time, learn Javascript first.

    Edited:

    This is always going to set it to cakes:
    Code:
    document.getElementById("yourmom").innerHTML = cakes;
    Like I said. Update it in the success function.

  24. Post #24
    KSI
    Gold Member

    July 2007
    3,310 Posts
    Okay, fair enough, can you please show me how to actually do that? I think I've tried just about everything, everything that is not the correct thing >__<

    My code:
    Code:
    var returnData = "cakes";
    
    	$(document).ready(function() {
    
    		$.ajax({
    
    		url: "jq_username.php?username=eax",
    
    		global: false,
    
    		type: "GET",
    
    		dataType: "text",
    
    		cache: false,
    
    		success: function(returnData, successCode, requestObject){
    
    			returnData = requestObject.responseText;
    
    			}
    
    	}
    
    );
    });
    
    
    document.getElementById("yourmom").innerHTML = cakes;
    Your last line will always set it to cakes, and you're not doing anything with the data returning from the ajax call.

  25. Post #25
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    Thanks for saying what I posted TehWhale.

  26. Post #26
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    I'll edit what I posted before:

    Code:
    <script id="source" language="javascript" type="text/javascript"> 
    	var returnData;
    	var userName = "eax";
    	
    	
    	$(document).ready(function() {
    		$(document).click(function(){
    			$.ajax({
    				  url: "jq_username.php?username=" + userName,
    				  global: false,
    				  type: "GET",
    				  dataType: "text",
    				  cache: false,
    				  success: function(returnData, successCode, requestObject){
    					returnData = requestObject.responseText;  	// Herp Derp this sets your return Data
    					 $("#field").html(returnData);				// This sets the HTML to to what it returned
    				  }
    			   }
    			);
    		});
    	});
    </script>
    Next time, learn Javascript first.

    Edited:

    This is always going to set it to cakes:
    Code:
    document.getElementById("yourmom").innerHTML = cakes;
    Like I said. Update it in the success function.
    Great thanks a lot : ) Yeah, just realized that, it was a leftover error for some test I did 5 minutes before my post.

    I is true, I need to learn proper javascript but so far I have been unable to find a proper place to learn it from :/
    Also, I still cannot use the data that I grabbed using Ajax, the returnData, outside of the jQuery function, it sets the HTML to what it returned but that was just for debugging, I need to check if the number is either 1 or 0 and then do something accordingly. Also, thanks a lot for helping :)

  27. Post #27
    KSI
    Gold Member

    July 2007
    3,310 Posts
    Thanks for saying what I posted TehWhale.
    You ninja'd me.

  28. Post #28
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    You ninja'd me.
    By two minutes? It doesn't take you that long to type a sentence.

    Edited:

    Great thanks a lot : ) Yeah, just realized that, it was a leftover error for some test I did 5 minutes before my post.

    I is true, I need to learn proper javascript but so far I have been unable to find a proper place to learn it from :/
    Also, I still cannot use the data that I grabbed using Ajax, the returnData, outside of the jQuery function, it sets the HTML to what it returned but that was just for debugging, I need to check if the number is either 1 or 0 and then do something accordingly. Also, thanks a lot for helping :)
    In the success function call another function by passing the data returned. Then just test if it equals 0 or 1. Best thing would be to get it to an Integer with parseInt(returnData). Then you can test if they're right or not.

  29. Post #29
    KSI
    Gold Member

    July 2007
    3,310 Posts
    By two minutes? It doesn't take you that long to type a sentence.
    Already had the page loaded, quoted and replied.Then yours popped up.

  30. Post #30
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    By two minutes? It doesn't take you that long to type a sentence.

    Edited:



    In the success function call another function by passing the data returned. Then just test if it equals 0 or 1. Best thing would be to get it to an Integer with parseInt(returnData). Then you can test if they're right or not.
    Hmm, can a function change a variable 'outside' of itself?
    I use the 1/0 evaluation to evaluate something else entirely :)

  31. Post #31
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    Can what function, change what?

  32. Post #32
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Can what function, change what?
    Can the function that I am going to create, that is going to run in the Success, change a variable declared out of it's scope?

  33. Post #33
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    Can the function that I am going to create, that is going to run in the Success, change a variable declared out of it's scope?
    Yes, that's why you can change returnData from the success function.

  34. Post #34
    Gold Member
    h2ooooooo's Avatar
    June 2007
    977 Posts
    Can the function that I am going to create, that is going to run in the Success, change a variable declared out of it's scope?
    If you make sure to "var name" the variable in the global scope (outside the function, and preferably in the top, then yes.)

  35. Post #35
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Yes, that's why you can change returnData from the success function.
    Hmm, I have tried this:
    Code:
    var returnData;
    
    var userName = "eax";
    
    	
    
    function changeData(varInput){
    
    	returnData = varInput;
    
    }
    
    	
    
    $(document).ready(function() {
    
    	$(document).click(function(){
    
    		$.ajax({
    
    		url: "jq_username.php?username=" + userName,
    
    		global: false,
    
    		type: "GET",
    
    		dataType: "text",
    
    		cache: false,
    
    		success: function(returnData, successCode, requestObject){
    
    		returnData = requestObject.responseText;  	// Herp Derp this sets your return Data
    
    		changeData(returnData);
    
    	$("#testField2").html(returnData);				// This sets the HTML to to what it returned
    
    	}
    
    }
    
    );
    
    });
    
    });
    
    
    document.getElementById("testField").innerHTML = returnData;
    But it changed neither testField2 nor testField,
    sorry if I seem stupid but I simply cannot wrap my head around this, what am I doing wrong?

  36. Post #36
    Gold Member
    andersonmat's Avatar
    May 2008
    2,607 Posts
    Code:
    changeData(returnData);
    Is setting returnData equal to returnData and for Christ's sake, stop adding this:

    Code:
    document.getElementById("testField").innerHTML = returnData;

  37. Post #37
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Code:
    changeData(returnData);
    Is setting returnData equal to returnData and for Christ's sake, stop adding this:

    Code:
    document.getElementById("testField").innerHTML = returnData;
    What should I set it to instead? Isn't returnData what we get from the Ajax call?

    Also, sorry about that.

    Edited:

    Another problem is that it doesn't update testField2 either :/

  38. Post #38
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Now, returnDataX is set to returnData, still doesn't work :/
    Code:
    var returnDataX;
    
    var userName = "eax";
    
    
    
    function changeData(varInput){
    
    	returnDataX = varInput;
    
    }
    
    
    
    $(document).ready(function() {
    
    	$(document).click(function(){
    
    		$.ajax({
    
    		url: "jq_username.php?username=" + userName,
    
    		global: false,
    
    		type: "GET",
    
    		dataType: "text",
    
    		cache: false,
    
    		success: function(returnData, successCode, requestObject){
    
    		returnData = requestObject.responseText;  	// Herp Derp this sets your return Data
    
    		changeData(returnData);
    
    						$("#testField2").html(returnData);				// This sets the HTML to to what it returned
    
    				}
    
    			}
    
    		);
    
    	});
    
    });

  39. Post #39
    Gold Member
    h2ooooooo's Avatar
    June 2007
    977 Posts
    Try to var returnData inside the function, as it's not var'd yet :)

  40. Post #40
    Eax
    Gold Member
    Eax's Avatar
    February 2006
    1,004 Posts
    Try to var returnData inside the function, as it's not var'd yet :)
    Thanks :) But it didn't help :/

    Code:
    $(document).ready(function() {
    
    			$(document).click(function(){
    
    				$.ajax({
    
    					url: "jq_username.php?username=" + userName,
    
    					global: false,
    
    					type: "GET",
    
    					dataType: "text",
    
    					cache: false,
    
    					success: function(returnData, successCode, requestObject){
    
    						var returnData;
    
    						returnData = requestObject.responseText;  	// Herp Derp this sets your return Data
    
    						changeData(returnData);
    
    						$("#testField2").html(returnData);				// This sets the HTML to to what it returned
    
    					}
    
    				}
    
    				);
    
    			});
    
    		});