0

I'm trying to parse a url in pure javascript, just one executable file.

   url = 'http://myurl.php?format=json'
   var request = new XMLHttpRequest();
   request.open('GET', url, true);

   request.onload = function() {
     if (request.status >= 200 && request.status < 400) {
       var mystuff = JSON.parse(request.responseText);
     } else {
       // some error
     }
   };
   request.onerror = function() {
     // some error
   };
   request.send();

   console.log(mystuff);

When I do this, I get a XMLHttpRequest is not defined error. What's the best way to do this, the simplest way?

Thank you.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Imalea
  • 376
  • 5
  • 14
  • what is the browser ? – Madhawa Priyashantha Nov 20 '16 at 02:25
  • I'm actually running this through hook.io, it's a Slack slash command app (Slack bot). – Imalea Nov 20 '16 at 02:28
  • 3
    @Imalea, quite simple: your environment does not have a definition of `XMLHttpRequest`. You will need to figure out what the alternative is for them. – Spencer D Nov 20 '16 at 02:36
  • Isn't there a way to do this in Javascript that wouldn't involve XMLHttpRequest? (which from what I understand only works in browsers) – Imalea Nov 20 '16 at 02:42
  • @Imalea, Javascript has two ways of performing AJAX: `XMLHttpRequest` and `new ActiveXObject("Microsoft.XMLHTTP")` (which is only existent on old internet explorer browsers). There are additional methods for performing AJAX in popular libraries (e.g., jQuery has `$.ajax()`), but even these rely on those two `XMLHttpRequest` and `new ActiveXObject("Microsoft.XMLHTTP")`. If those are not available in your environment and your environment does not provide an alternative option (e.g., the ability to curl, which might be available in your environment), then you are pretty much out of luck. – Spencer D Nov 20 '16 at 02:51
  • I'm not familiar with hook.io, so it is possible that there is an alternative. Perhaps someone that knows more about hook.io can shed some light on this? – Spencer D Nov 20 '16 at 02:52

1 Answers1

0

This statement is wrong you should url as a variable not "url" as a string,

request.open('GET', 'url', true);

to

request.open('GET', url, true);

Furthermore the xmlHttpRequest works only on some versions of browsers. You could do something like this to check whether xmlhttpRequest works on your browser,

var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
 }
GraveyardQueen
  • 771
  • 1
  • 7
  • 17
  • 4
    I agree the 'url' thing is an error, but I doubt that resolves the issue of XMLHttpRequest not being defined, unless the OP's environment produces entirely unrelated error messages. – Spencer D Nov 20 '16 at 02:37
  • Thanks @GraveyardQueen, that's a typo on my end in stackoverflow, not how it is in my code. Definitely not the issue. – Imalea Nov 20 '16 at 02:41
  • which browser and version are you using?@Imalea – GraveyardQueen Nov 20 '16 at 02:44
  • I'm using hook.io to run the script, and testing it in slack (it's a slash command app). – Imalea Nov 20 '16 at 02:50
  • Although the underlying problem here is that he is not running in a browser, just out of curiosity in what browsers do you believe `XMLHttpRequest` is not available? –  Nov 20 '16 at 03:09