views:

54

answers:

4
+1  Q: 

jQuery.Get() help

Hey,

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 the above is wrong, can someone help my out here?

Cheers,

+1  A: 

try to use this:

$.get("test.php", function(data){
   alert("Data Loaded: " + data);
 });
czerasz
A: 

Is http://mysite..... on the same domain as the jQuery $.get(..) ??

If not, you'll get a cross-domain error.

Adam
Yes it is. My example returns [object XMLHTTPRequest], could this be the problem?
AveragePro
+1  A: 

$.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.

lonesomeday
A: 

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.

PriorityMark