• Passing data from jQuery(ajax) to Javascript
    68 replies, posted
  • Avatar of Eax
  • Hi there, I have the following jQuery code: [code] $(function() { $.ajax({ url: siteCall, success: function(data, textStatus, XMLHttpRequest) { returnData = data; } }); }); [/code] 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!
  • Avatar of Zaldos
  • 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> [/code] Or call a function you have created to do something with the data.
  • Avatar of Eax
  • [QUOTE=Zaldos;22370614]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> [/code] Or call a function you have created to do something with the data.[/QUOTE] 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; } }); }); [/code] Are you sure it's put in the right place in the jQuery?
  • Avatar of Zaldos
  • try [code] returnData = ""; [/code] not [code] var returnData; [/code]
  • Avatar of Eax
  • [QUOTE=Zaldos;22371110]try [code] returnData = ""; [/code] not [code] var returnData; [/code][/QUOTE] That didn't work either :S And I get no errors :/ It returns the ""?
  • Avatar of Zaldos
  • Have a look at this. [url="http://valisto.com/dev/index.php"]http://valisto.com/dev/index.php[/url] It seems to work for me.
  • Avatar of Eax
  • [QUOTE=Zaldos;22371483]Have a look at this. [url="http://valisto.com/dev/index.php"]http://valisto.com/dev/index.php[/url][/QUOTE] Weird! I am trying "sort of" the same here: [url]http://127.0.0.1/stock/debug_ajax.php[/url] 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
  • Avatar of Zaldos
  • No I cant. Its a local host. Not your external url. But if you want to manipulate look at my url again.
  • Avatar of Eax
  • Oh shit! Forgot to put it on my FTP SORRY! XD Here's the real link: [url]http://stock.eax.dk/debug_ajax.php[/url] Sorry!
  • Avatar of Zaldos
  • Try the url [url]http://stock.eax.dk/jq_username.php?username=eax[/url] I think it has something to do with the url directory. Or I may be mistaken.
  • Avatar of Eax
  • Hmm how so? They are in the same directory if that is what you mean?
  • Avatar of Eax
  • [QUOTE=Zaldos;22375859]I really don't know sorry.[/QUOTE] Fair enough, thanks anyway! : ) Anyone else knows? : )
  • Avatar of Eax
  • [QUOTE=andersonmat;22376027]I get cakes when I view the page. It looks like you're doing it correctly.[/QUOTE] Cakes is what it starts with, it should replace the cakes with "1" :S
  • Avatar of andersonmat
  • [QUOTE=Eax;22376039]Cakes is what it starts with, it should replace the cakes with "1" :S[/QUOTE] Oh, lemme look again. :v: [editline]05:09PM[/editline] Hey mate, here you go. Make sure the file is server-side. Then just set the variable to what you need it to be: [url]http://srcsrv.com/test.php[/url] [editline]05:11PM[/editline] P.s: [b]Click![/b]
  • Avatar of Darkimmortal
  • 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.
  • Avatar of andersonmat
  • [QUOTE=Darkimmortal;22382010]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.[/QUOTE] Unless you block. Which is dumb. Very, very dumb.
  • [QUOTE=Zaldos;22371110]try [code] returnData = ""; [/code] not [code] var returnData; [/code][/QUOTE] [B]PLEASE DO NOT DO THIS.[/B] 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";[/code] [B]OH AND:[/B] :siren: jQuery is not a separate language from JavaScript, it is a set of functions and methods for use in JavaScript. :siren:
  • Avatar of Eax
  • [QUOTE=andersonmat;22376079]Oh, lemme look again. :v: [editline]05:09PM[/editline] Hey mate, here you go. Make sure the file is server-side. Then just set the variable to what you need it to be: [url]http://srcsrv.com/test.php[/url] [editline]05:11PM[/editline] P.s: [b]Click![/b][/QUOTE] 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; [/code] But it still writes out 'Cakes' :S Any idea what is wrong? [QUOTE=a2h;22382272][B]PLEASE DO NOT DO THIS.[/B] 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";[/code] [B]OH AND:[/B] :siren: jQuery is not a separate language from JavaScript, it is a set of functions and methods for use in JavaScript. :siren:[/QUOTE] Okay I won't do that! :) Yes I know it's a set of functions and methods :)
  • Avatar of andersonmat
  • It's ALWAYS going to print Cakes. It's asynchronous for a reason. You need to update it in the AJAX success function.
  • Avatar of Eax
  • [QUOTE=andersonmat;22406292]It's ALWAYS going to print Cakes. It's asynchronous for a reason. You need to update it in the AJAX success function.[/QUOTE] 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; [/code]
  • Avatar of andersonmat
  • 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> [/code] Next time, learn Javascript first. [editline]01:09PM[/editline] This is always going to set it to cakes: [code] document.getElementById("yourmom").innerHTML = cakes; [/code] Like I said. Update it in the success function.
  • [QUOTE=Eax;22415255]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; [/code][/QUOTE]Your last line will always set it to cakes, and you're not doing anything with the data returning from the ajax call.
  • Avatar of Eax
  • [QUOTE=andersonmat;22418626]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> [/code] Next time, learn Javascript first. [editline]01:09PM[/editline] This is always going to set it to cakes: [code] document.getElementById("yourmom").innerHTML = cakes; [/code] Like I said. Update it in the success function.[/QUOTE] 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 :)
  • [QUOTE=andersonmat;22418711]Thanks for saying what I posted TehWhale. [img]http://i32.tinypic.com/33upd14.jpg[/img][/QUOTE]You ninja'd me.
  • Avatar of andersonmat
  • [QUOTE=KSI;22419006]You ninja'd me.[/QUOTE] By two minutes? It doesn't take you that long to type a sentence. [editline]01:30PM[/editline] [QUOTE=Eax;22418855]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 :)[/QUOTE] 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.
  • [QUOTE=andersonmat;22419171]By two minutes? It doesn't take you that long to type a sentence.[/QUOTE] Already had the page loaded, quoted and replied.Then yours popped up.
  • Avatar of Eax
  • [QUOTE=andersonmat;22419171]By two minutes? It doesn't take you that long to type a sentence. [editline]01:30PM[/editline] 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.[/QUOTE] Hmm, can a function change a variable 'outside' of itself? I use the 1/0 evaluation to evaluate something else entirely :)