Can someone explain why the warning statement does not return anything when used in this context:
$(document).ready(function(){ $.get("/getsomedata.php", function(data){ $("#mydiv").append(data) }); alert($("#mydiv").html()); //outputs nothing });
When this statement returns what you expect:
$(document).ready(function(){ $("#mydiv").append('some info') alert($("#mydiv").html()); //outputs 'someinfo' });
.get does not block, the rest of your script continues to run while the document is loading. To make it work the way you expect, put a warning inside the anonymous function that you provide. This is a callback function and will run after loading a document. i.e:
$(document).ready(function(){ $.get("/getsomedata.php", function(data){ $("#mydiv").append(data); alert($("#mydiv").html()); // This won't fire til somedata.php is loaded }); });
. function(data){$("#mydiv").append(data)} - , . . , , , "".
function(data){$("#mydiv").append(data)}
, , . , , .
- :
$(document).ready(function(){ $.get("/getsomedata.php", function(data){ $("#mydiv").append(data); alert($("#mydiv").html()); // will show your data }); });
AJAX- ( ), , AJAX ( ), , html, , .
, , HTML DIV , $.get()
$(document).ready(function(){ $.get("/getsomedata.php", function(data){ $("#mydiv").append(data) alert($("#mydiv").html()); //outputs nothing }); });
. , , html GET.
AJAX , , . , $.get(), " ", - , . , , :
$.get()
$(document).ready(function(){ $.get("/getsomedata.php", function(data){ // this is a "callback" $("#mydiv").append(data) alert($("#mydiv").html()); }); });
get, , XMLHttpRequest -
, , , div, , ocurs
Source: https://habr.com/ru/post/1727675/More articles:threadafe single-user, single-profile FIFO on embedded system - thread-safetyCan I manage an HTTP response response server from a server? - httpWatermark PDF only for printing, software - printingprocessing complex results in objects - exceptions? - oopIs there any OpenSSL function to convert a PKCS7 file to a PEM - c ++Type mismatch error causing C # dll from VBA using com interop - c #tomcat or glassfish as a comet server? - javaSetting a value through reflection does not work - reflectionConvert string to linq query - dynamicErlang init error with tsung-recorder - erlangAll Articles