在不加载网页的情况下在后台执行HTTP请求? [英] Perform an HTTP request in the background without loading webpage?

查看:122
本文介绍了在不加载网页的情况下在后台执行HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,该第三方网站包含一个按钮,单击该按钮后,该按钮将发送GET请求并执行一些服务器端任务,该任务会将已登录用户的数据更新到其数据库中.必须在每个日历日执行此期望的任务.有没有其他方法可以使该网站的服务器执行任务,而不需要在每个日历日都加载该页面?附加到该按钮的URL似乎正在使用REST API,因为按钮元素具有href URL,该URL包含路径+自变量.目前,我要使其执行服务器端任务的解决方案是更改URL的参数并在新的浏览器选项卡中打开该URL.

So there is this 3rd party website that contains a button that, when clicked, sends a GET request and performs some server-side task which updates the logged in user's data into its database. This desired task has to be performed on every calendar day. Is there an alternative way to making this website's server perform the task without making it load the page for each of the calendar days? The URL attached to that button seems to be using REST API because the button element has href URL which contains a path + arguments. Currently, my solution to making it perform that server-side task for me is to change the URL's argument and open that URL in a new browser tab.

更精确地说,URL看起来像这样:https://something.com/somepath/47240/6/sort?date=2018-03-06&crumb=JMBuREVDPqS 我发现只需将date参数更改为第二天并将该URL加载到浏览器中,该网站的服务器也将在第二天执行所需的任务.有没有办法让它在后台执行此任务而无需每天加载单独的选项卡,还是操纵date参数并在单独的选项卡中打开更新的URL是唯一的解决方案?由于我当前的方法只是打开更新的日期URL,因此它是完全通过客户端代码实现的.因此,我想知道仅使用客户端代码的替代解决方案.

To be more precise the url looks something like this: https://something.com/somepath/47240/6/sort?date=2018-03-06&crumb=JMBuREVDPqS I discovered that simply changing the date argument to the next day and loading that URL in the browser will make this website's servers perform the desired task for the next day too. Is there a way to make it perform this task in the background without loading a separate tab for each day or is manipulating the date argument and opening that updated URL in a separate tab the only solution? Since my current method simply opens up the updated date URLs, it was implemented purely through client-side code. So I would like to know of alternative solutions using client-side code only.

推荐答案

  1. 生成要调用的日期数组.
  2. 遍历每个日期,然后向服务器发出请求.
  3. 请求完成后,选择下一个日期并进行处理.

以下是示例代码,您可以根据情况进行修改.

Following is the sample code, which you can modify according to your case.

  var url="https://something.com/somepath/47240/6/sort?date=";
  var date1 = new Date();
  var date2 = new Date(2018, 1, 1);
  var day;
  var between = [date1];
 //Generate the date array for which you want to run
  while(date2 <= date1) {
      day = date1.getDate()
      date1 = new Date(date1.setDate(--day));  
      between.push(date1);
  }
//Covert date to string format for passing to the request url
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    return [year, month, day].join('-');
  }

  var callPage = function (dt) {
       return new Promise(function (resolve, reject) {
          var xhr = new XMLHttpRequest();
          var urlWithParam=url+ dt + "&crumb=JMBuREVDPqS";
          xhr.open('GET', urlWithParam, true);
          xhr.onload = function () {
            //Resolve the promise, so that next can be picked
             resolve();
          };

        });
      };
    //Make the call for each date.
    for (var i = 0; i < between.length; i++) {
        callPage(between[i]).then(function () {
      });
    }

如果您遇到CROSS DOMAIN请求的问题,可以使用隐藏的iframe来实现此目标,如下所示.

If you are facing issue with CROSS DOMAIN request, you can achieve this using hidden iframe like following.

 var url="https://something.com/somepath/47240/6/sort?date=";
  var date1 = new Date();
  var date2 = new Date(2018, 1, 1);
  var day;
  var between = [date1];
 //Generate the date array for which you want to run
  while(date2 <= date1) {
      day = date1.getDate()
      date1 = new Date(date1.setDate(--day));  
      between.push(date1);
  }
//Covert date to string format for passing to the request url
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    return [year, month, day].join('-');
  }
    
  var callPage = function (dt) {
    
    var iframe = document.createElement('iframe');
    var urlWithParam=url+ formatDate(dt) + "&crumb=JMBuREVDPqS";
    console.log("Request ->",urlWithParam);
    iframe.style.display = 'none';
    iframe.src = encodeURI(urlWithParam);
    document.body.appendChild(iframe);
  }    
    //Make the call for each date.
    for (var i = 0; i < between.length; i++) {
        callPage(between[i]);
    }

朋克

这篇关于在不加载网页的情况下在后台执行HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆