1

How can I execute a simple webrequest in javascript.

Such as

var str = *whatever_comes_back_from*("search.php?term=hello");
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Roger
  • 6,443
  • 20
  • 61
  • 88
  • possible duplicate of [HTTP GET request in JavaScript?](http://stackoverflow.com/questions/247483/http-get-request-in-javascript) – Jason C Mar 24 '14 at 05:43

2 Answers2

4

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

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)
});
Billy Moon
  • 57,113
  • 24
  • 136
  • 237