Roger
Roger

Reputation: 6527

javascript - how to execute a simple GET webrequest?

How can I execute a simple webrequest in javascript.

Such as

var str = *whatever_comes_back_from*("search.php?term=hello");

Upvotes: 1

Views: 5547

Answers (2)

Billy Moon
Billy Moon

Reputation: 58531

You could use jQuery, or another javascript library, but instead of thinking of populating the variable then continueing with the script in a linear way, you should think in terms of a callback once the value is retrieved, because it can take a variable amount of time to retrieve the data.

This event based architecture is a feature of javascript that is rare in other programming languages.

$.get('search.php?term=hello', function(data){
    alert(data)
});

Upvotes: 2

Quentin
Quentin

Reputation: 943470

This is usually handed via XMLHttpRequest, usually abstracted via a library that irons out the differences between browsers (bigger libraries that do lots of other stuff include YUI and jQuery).

Upvotes: 4

Related Questions