Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

javascript - Why can I not use new XMLHttpRequest on my amazon server?

So bear with me because I feel that I've tried everything there is to try. I'm really new to JavaScript and I've primarily worked with C/C++ before and I am still a novice with respect to those languages as well. I tried to create a page on my aws services (ec2) server that when accessed with a get request of a URL it would use my server-side API key and return JSON data that it requested from the URL given. I was working with the League of Legends API and with NodeJs using forever start *.js, handlebars and express JavaScript as well.

I feel that it is important to mention that while this issue arose doing an assignment for a class I have since completed the assignment without including that functionality.

I tried to do this as both synchronous and asynchronous and it didn't work for me. I feel that it is because using var foo = new XMLHttpRequest is not the correct way to send a get request from a server to another server. When I tried to find a better way to do this using nodejs I could not find anything which made sense to me or that I could understand.

Please let me know why this is wrong/ why this breaks and how I can make it not break or do it properly. I couldn't find any similar stack overflow questions despite a few hours of struggling and searching.

app.get('/test',function(req,res){

       //if(url == undefined){
    var url = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/RiotSchmick";
       //}
    var KEY= "REDACTED_FOR_POST";//note I have a functional api 
       //key that I can correctly use to view data through a browser get request.

    var local = new XMLHttpRequest();//this line prevents this page from runnning

    local.open("GET", "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/RiotSchmick?api_key=REDACTED_FOR_POST", false);

       //RiotSchmick is used as an example "summoner" name in quick start
    local.send(null);

    var content = {};//holder for handlebars
    content.text = local.responseText;//I expected this to have a string form JSON.

    res.render('buffer.handlebars', content);
       //buffer.handlebars is one line: {{text}} and properly prints data when
       //content.text is assigned to a constant such as "hello"

})

Thanks for help and your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unless you are using something like https://github.com/driverdan/node-XMLHttpRequest there's no XMLHttpRequest in node. XMLHttpRequest is an object that only exists in browsers.

You have to require http to make http requests in node.

var http = require('http');

Then you can do something like that (taken from the docs):

var postData = querystring.stringify({
  'msg' : 'Hello World!'
});

var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};

var req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.')
  })
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end(

);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...