How do I use jQuery.Get() to return web contents?
I'm trying to use the jQuery.Get() function to return the contents of a webpage.
Something along the lines of -
var data = $.get("http://mysite...../x.php");
I know开发者_如何学Python the above is wrong, can someone help my out here?
try to use this:
$.get("test.php", function(data){
alert("Data Loaded: " + data);
});
$.get does not return the result of the query. It is an AJAX call and the first A in AJAX stands for "asynchronous". That means that the function returns before the AJAX request is complete. You therefore need to supply a function as the second argument of the call:
var data = $.get("http://mysite...../x.php", function(data) {
alert(data);
});
See http://api.jquery.com/jQuery.get/ for more examples and options that you can set.
jQuery ajax calls are asynchronous, so you'll need to do something on the callback of the get.
$.get('http://mysite...../x.php', function(data) { $('.result').html(data); alert('Load was performed.'); });
Also, bear in mind that $.get is just a convenience handler. Even the documentation (http://api.jquery.com/jQuery.get/) indicates that it calls $.ajax. With that in mind, it's always a better idea to call the method directly, as it's a couple less calls pushed on to the stack and you save a few CPU cycles.
精彩评论