简体   繁体   中英

how to get data from asp.net api restful webservice from node.js server?

I try to get data from my asp.net restful webservice from my node.js server but I dont know how to do it. I have tried some ways (using jquery, but get some error, and I dont know why). The url to get data from asp.net restful is something like this: "http://localhost:20034/api/Homepage/friendlist?id=1"

Hope someone can help me. Thanks!

Not sure about that error you are getting when using jQuery. You would need to provide more details about that.

In general though, if you want to make an HTTP request using node.js, you can use the http.get() function.

For example (from the official node.js documentation):

var http = require('http');
http.get("http://www.google.com/index.html", function(res) {
  console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

If you wanted to use that in a separate function you would call it and provide a callback. This is important due to the asynchronous nature of the http.get() call. Here is an example:

var http = require('http');

var callService = function(url, callback) {
    http.get(url, function(res) {
        return callback(null, res);
    }).on('error', function(err) {
        return callback(err);
    });
}

callService("http://www.google.com/index.html", function(err, res){
    if (err) console.log("Got error: " + err.message);
    else console.log("Final result:" + res);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM