Node.js 在nodejs中将AJAX请求转换为HTTP请求

Node.js 在nodejs中将AJAX请求转换为HTTP请求,node.js,express,Node.js,Express,实际上,当我在浏览器上使用api时,我正在使用ajax调用连接到第三方api,并且我能够从api获取数据。 以下是AJAX代码:- var settings = { async: true, //crossDomain: true, url: "https://rest.cricketapi.com/rest/v2/schedule/?access_token=XXX" //"url": "https://rest.cricketapi.com/rest/v2/match/ind

实际上,当我在浏览器上使用api时,我正在使用ajax调用连接到第三方api,并且我能够从api获取数据。 以下是AJAX代码:-

var settings = {
  async: true,
  //crossDomain: true,
  url: "https://rest.cricketapi.com/rest/v2/schedule/?access_token=XXX"
  //"url": "https://rest.cricketapi.com/rest/v2/match/indaus_2020_one-day_02/?access_token=XXX"
  //bblt20_2019_g28
};

//create token for the api for 24hours
let token;
function getToken() {
  $.ajax({
    type: "POST",
    url: "https://rest.cricketapi.com/rest/v2/auth/",
    data: {
      access_key: "********************************",
      secret_key: "********************************",
      app_id: "http://localhost:8000/",
      device_id: "developer"
    },
    success: function(data) {
      console.log(data);

      token = data.auth.access_token;
      console.log(token,);
      //do something when request is successfull
      createUpcomingMatchesSchedule();
    },
    dataType: "json"
  });
}

function createUpcomingMatchesSchedule() {
  var urlJson = settings.url;
  urlJson = urlJson.replace(/XXX/g, token);
  settings.url = urlJson;

  $.ajax(settings).done(function(response) {
    console.log(response);
    toGetUpcomingMatches(response);
  });
}

现在,我想将这个ajax方法转换为http请求,以便使用nodejs使用服务器上的数据。 代码如下:-

const { db } = require("../../utility/admin");
var request = require("request");

// //*************************** API INITIALIZATION ********************************

var settings = {
  async: true,
  //crossDomain: true,
  url: "https://rest.cricketapi.com/rest/v2/schedule/?access_token=XXX"
  //"url": "https://rest.cricketapi.com/rest/v2/match/indaus_2020_one-day_02/?access_token=XXX"
  //bblt20_2019_g28
};

var options = {
  method: "POST",
  // hostname: "https://rest.cricketapi.com",
  uri: "https://rest.cricketapi.com/rest/v2/auth/",
  access_key: "********************************",
  secret_key: "********************************",
  app_id: "http://localhost:8000/",
  device_id: "developer"
};

let token;
function getToken() {
  request(options, function(error, response, body) {
    if (error) {
      console.log(error);
    } else {
      console.log(body);
      token = body.auth.access_token;
      console.log(token);
      // console.log(response);
    }
  });
}

module.exports = { getToken };


但是,当我使用nodejs代码时,我无法从api访问数据。
提前感谢

您面临什么问题?您是否有任何错误?您使用的API为nodejs开发人员提供了文档。你可能应该遵循它。jQuery为您设置许多http请求头,如
Accept
Content-Type
。它还为您构造POST有效负载及其
内容长度
标题值。所有这些内容都以不同于nodejs的方式处理。@O.Jones实际上在他们的api中使用curl命令。他们没有nodejs命令。对于web,我已将他们的curl命令转换为ajax调用,但现在我想在服务器端使用api。@KAMLESHKUMAR没有收到错误,但没有收到api的响应。我已经添加了我的答案,您可以检查并测试它。我希望它能帮助你。出于兴趣,你为什么开始在答案中添加表情符号?似乎。。。分散注意力。(我没有投反对票)没关系。我认为这很烦人,这取决于人们如何判断。至于我,没有多少。这很平常。对于downvote,我之前的回答中有一些错误,然后我只是更新了它。你认为这很烦人,所以你把它们放进去了?我认为这是可以接受的,所以不应该出现在帖子中。我得到了以下错误:{>状态:false,>cachekey:null,>version:'2.0.2',>status\u code:403,>status\u msg:'Missing Credentials',>expires:'-1',>Etag:null,>data:null>}>C:\Users\Aman\Desktop\Predicta\u Project\Predicta Cloud Functions\Functions\handlers\LiveScoring\scoring.js:35>token=body.auth.access\u token;>^>>类型错误:无法读取未定义的@Titus Sutio Fanpula的属性“access\u token”