首页 > 解决方案 > 在nodejs中将AJAX请求转换为HTTP请求

问题描述

实际上,当我在浏览器上使用 api 并且我能够从 api 获取数据时,我正在使用 ajax 调用连接到第 3 方 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 访问数据。提前致谢

标签: node.jsexpress

解决方案


‍举个例子,你可以看下面这段代码:

var options = {
  method: "POST",
  // hostname: "https://rest.cricketapi.com",
  uri: "https://rest.cricketapi.com/rest/v2/auth/",
  // make sure, on this API, use body or headers. 
  // If body, you can change this `headers` to `form`.
  headers: { 
    access_key: "********************************",
    secret_key: "********************************",
    app_id: "http://localhost:8000/",
    device_id: "developer"
  }
};

let token;
function getToken() {
  return new Promise((resolve, reject) => {
    request(options, function(error, response, body) {
      if (error) return reject(error) 
      body = JSON.parse(body);
      console.log(body);
      token = body.auth.access_token;
      return resolve(token);
    });
  })
}

我希望它可以帮助你。


推荐阅读