[英]Node.js, How to get JSON elemet from some url which i have to enter username and password in it?
I use this function:
我使用这个 function:
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
function call:
function 调用:
getJSON('http://localhost:8080/job/'+jobName+'/lastBuild/api/json?tree=timestamp',
function(err, data) {
if (err !== null) {
console.log('Something went wrong: ' + err);
} else {
console.log('Your query count: ' + data.query.count);
}
});
I am getting 403 error witch indicates for credentials required
我收到 403 错误,表示需要凭据
You can do it using axios library.您可以使用 axios 库来完成。
var axios = require("axios");
var url = "your-url";
async function getData(url) {
try {
var response = await axios.get(url);
var data = response.data;
console.log(data);
} catch (error) {
console.log(error);
}
};
getData(url);
You need to install axios library using:您需要使用以下命令安装 axios 库:
npm install axios --save
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.