简体   繁体   English

Node.js, 如何从一些 url 中获取 JSON 元素,我必须在其中输入用户名和密码?

[英]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.

相关问题 Node.js是否从具有用户名和密码的URL下载文件? - Node.js download a file from a URL with a username and password? 如何从Node.js / Express中的URL获取JSON对象 - How to GET JSON Object from URL in Node.js/Express 如何从 Z28A3689BE95C88DD5E7A37DB516AB08 上的 API url 获得 json? - How can I get json from an API url on node.js? 我需要为将要接受数组的 get 创建 url,如何在 node.js/express 中从请求中提取数组? - I need to create url for get which is going to accept array, how in node.js/express extract array from request? 如何将我从 axios 获得的这些数据传递到使用 node.js(react 和 node.js)的后端 - How do I pass this data i get from axios to the backend which uses node.js (react & node.js) 如何验证 SMTP 主机名/端口/用户名/密码是否在 Node.JS 上工作? - How can I validate SMTP hostname/port/username/password is working on Node.JS? Node.js:如何从请求正文中获取到称为POST的页面的URL? - Node.js: how to get the URL to the page which called a POST from the request body? 将请求模块与node.js一起使用时,如何避免存储用户名/密码 - How do I avoid storing username/password when using request module with node.js 如何使用 Node.js 安全地存储数据库的 IP 地址、用户名和密码? - How can I securely store the IP address, username and password of a database using Node.js? 我的 Node.js/Express.js 项目中有一个公共文件夹,其中包含一些图像。 当我尝试通过 URL 访问这些图像时,出现 404 错误 - I have a public folder in my Node.js/Express.js project which contains some images. When I try to access those images through URL, I get 404 error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM