Search code examples
javascriptpythonurllib

python's urllib.urlopen for javascript


Is there a module written in javascript that's equivalent to python's urllib? In particular, I want something like:

urllib.urlopen(url, data)

which returns an object that supports a blocking fetch. Alternatively, in what other ways could I perform a blocking request to a server using javascript?


Solution

  • You can use the normal XMLHttpRequest synchronously:

    var xhr = new XMLHttpRequest();
    xhr.open('POST', url, false);  # third param is sync/async
    xhr.send(data);
    var response = xhr.responseText;