0

I need to call http request cross-domain which I can consume successfully by $.ajax with code below. The request bases on Basic Authentication, it works.

$.ajax({
    type: "GET",
    url: "https://" + username + ":" + password + "@/api/systems",
    //crossDomain: true,
    dataType: 'jsonp',
    async: true
}).done(function (result) {
    $("#div1").html(JSON.stringify(result.data));
});  

However, I can't get it to work in nodejs running in server. My code is as below:

var request = require('request');
var url = "https://" + username + ":" + password + "@/api/systems";
var option = {
    url: url,
    auth: {
        user: username,
        password: password
    }
};
request(option,
    function (error, response, body) {
        console.log(body);
    }
);

It always returns Error 401 Unauthorized.

How can I get it in nodejs?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
duongkha
  • 71
  • 6

1 Answers1

0

Use the npm for "cors", use it as middleware https://www.npmjs.com/package/cors

Kfir Bloch
  • 68
  • 2
  • 6
  • i enabled cors already but still no luck. I see that the authentication header is not sent to requested server yet. Do you have idea to fix it? – duongkha Mar 01 '17 at 06:09