AveragePro
AveragePro

Reputation: 1081

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

Upvotes: 1

Views: 310

Answers (3)

PriorityMark
PriorityMark

Reputation: 3247

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.

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237865

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

Upvotes: 1

czerasz
czerasz

Reputation: 14260

try to use this:

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

Upvotes: 1

Related Questions