1

How can I execute a simple webrequest in javascript.

Such as

var str = *whatever_comes_back_from*("search.php?term=hello");
Daniel Hilgarth
  • 166,158
  • 40
  • 312
  • 426
Roger
  • 6,307
  • 20
  • 58
  • 86
  • 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
  • 857,932
  • 118
  • 1,152
  • 1,264
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
  • 54,763
  • 23
  • 128
  • 231