简体   繁体   English

在没有服务器的情况下处理POST或GET请求

[英]Processing a POST or GET request without a server

I was going through the following article : 我正在阅读以下文章:

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Fetching_data https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Fetching_data

Here , the concept of AJAX is being illustrated however , for simple illustration ,instead of connecting to the server ,the content is being fetched from the system which has the browser in it . 在这里,将说明AJAX的概念,但是为了简单说明起见,不是从服务器连接内容,而是从装有浏览器的系统中获取内容。

So in the following code lines from the above mentioned link : 因此,在上述链接的以下代码行中:

var url = verse + '.txt';
var request = new XMLHttpRequest();
request.open('GET', url);

Here a GET verb is to fetch the contents of the file in the local system and no server is present there . 这里的GET动词是在本地系统中获取文件的内容,并且那里没有服务器。

Similarly , by using javascript and in the absence of a server can we add some parameters to GET or POST verb and run a code in the local system which processes these parameters and sends an output . 类似地,通过使用javascript并在没有服务器的情况下,我们可以向GET或POST动词添加一些参数,并在本地系统中运行代码来处理这些参数并发送输出。

Like : 喜欢 :

var url =  'verse + '.txt' + '?' 'name = ' + 'vim' ; //Adding parameters

and there will be some javascript file , which takes these parameter "name " and returns it in uppercase , like "VIM " . 并且将有一个javascript文件,该文件带有这些参数“ name”并以大写形式返回,如“ VIM”。

Can we do anything like that using Javascript only (not nodejs or anything that sets up a server " ) without server listening ? 我们是否可以仅使用Javascript来做类似的事情(而不是使用nodejs或用于设置服务器的任何事情)而无需服务器侦听?

To achieve the requirement you can use Chromium or Chrome browser launched with --allow-file-access-from-files flag set. 要达到此要求,您可以使用Chromium或Chrome浏览器(已设置--allow-file-access-from-files标志设置)启动。

fetch() does not permit requesting local files having file: protocol, though XMLHttpRequest() does. 尽管XMLHttpRequest()允许,但fetch()不允许请求具有file:协议的本地文件。 fetch() does allow requesting data URL and Blob URL . fetch()确实允许请求data URLBlob URL

For 对于

some javascript file , which takes these parameter "name " and returns it in uppercase , like "VIM " 一些javascript文件,该文件接受这些参数“ name”并以大写形式返回,例如“ VIM”

Worker can be used to get the contents of a local file, manipulate the content, then postMessage() can be called to communicate with main thread. 可以使用Worker获取本地文件的内容,操作内容,然后可以调用postMessage()与主线程进行通信。

For example 例如

worker.js worker.js

onmessage = e => {
  // do stuff
  let url = new URL(e.data);
  let [[,param]] = [...url.searchParams]; // get query string parameter `'vim'`
  let request = new XMLHttpRequest();
  request.open('GET', 'file:///path/to/local/file' /* e.data */);
  request.onload = e => {
    console.log(request.responseText); // `setTimeout()` can be inside `load` handler
  }
  request.send();
  // asynchronous response
  setTimeout(() => {
    // set `data URL` that `fetch()` will request
    postMessage(`data:text/plain,${param.toUpperCase()}`);
  }, Math.floor(Math.random() * 2000));
}

At console or within a script consolescript

const localRequest = async(url) => {
  const request = await fetch(await new Promise(resolve => {
    const worker = new Worker('worker.js');
    worker.onmessage = e => {
      resolve(e.data);
      worker.terminate();
    }
    worker.postMessage(url);
  }));
  const response = await request.text();
  console.log(response);
}

localRequest('file:///verse.txt?name=vim');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM