Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以在fetch选项中传递数据?_Javascript_Reactjs_Express_Cors_Fetch - Fatal编程技术网

Javascript 是否可以在fetch选项中传递数据?

Javascript 是否可以在fetch选项中传递数据?,javascript,reactjs,express,cors,fetch,Javascript,Reactjs,Express,Cors,Fetch,我有一个客户端应用程序 我想用express来包装它,这样当我从API获取数据时就不会出现CORS问题 现在,我所有的fetches都发生在客户端,带有一些端点URL 如果我错了,请纠正我。 为了正确地重新写入回迁请求,我从路由端点回迁,我将在我的server.js中定义该端点,对吗 在我的server.js中,我将从API中获取实际的fetch,对吗 如果是的话,我还有一个问题: 在我的客户端中,我有一些逻辑将决定fetch的url如何 我的问题:是否有一种方法可以将正确的url作为某种数据传

我有一个客户端应用程序

我想用
express
来包装它,这样当我从API获取数据时就不会出现
CORS
问题

现在,我所有的
fetch
es都发生在客户端,带有一些端点URL

如果我错了,请纠正我。 为了正确地重新写入回迁请求,我从路由端点回迁,我将在我的
server.js
中定义该端点,对吗

在我的
server.js
中,我将从API中获取实际的
fetch
,对吗

如果是的话,我还有一个问题:

在我的客户端中,我有一些逻辑将决定
fetch的url如何

我的问题:是否有一种方法可以将正确的url作为某种数据传递到我的获取请求中,这样我就可以在服务器端使用它来进行实际的API获取

下面是我的代码示例,如果可能的话,我会尽力理解:

const { current, amount, page} = this.pagination;
const { client_id, search } = this.API;

const limit = `_limit=${amount}`;
let currentPage;
let url;

if(this.isFetching) return;

if(direction == 'next'){
   this.pagination.current = current + amount;
   currentPage = page + 1
   let offset = `_offset=${this.pagination.current}`;        
} else {
if(page !== 1) {
    this.pagination.current = current - amount;
    currentPage = page - 1
    let offset = `_offset=${this.pagination.current}`;           
} 

url = `${search}${client_id}&${limit}&${offset}`;   //  Here is the final url

fetch('/api', {data: url})   //  PSUEDO use of fetch, is it possible?
    .then(response => response.json())       
         .then(data => {
             this.fetchedProperties = [...data.search_results];
             this.pagination.page = currentPage; 
             this.isFetching = false;                                  
         });
}

将数据从客户端传递到HTTP端点的正常方法是将其编码为查询字符串

http://example.com/fetch?url=http%3A%2F%2Fwww.example.net%2Ffoo%2Fbar%3Fquery%3Dsomething%26etc%3Detc