TypeError: Failed to execute 'fetch' on 'Window': Invalid value

后端 未结 8 552
情歌与酒
情歌与酒 2020-12-24 14:24

I\'ve tried to use fetch to call from backend using react, without libs (such as Axios). So I created this function:

export function api(url, method, body, i         


        
相关标签:
8条回答
  • 2020-12-24 15:23

    In my case it occurred because I wrote "Content Type" instead of "Content-Type"

    0 讨论(0)
  • 2020-12-24 15:27

    I got the same problem, but working with Angular 7, I was using a Interceptor service like this:

    // headerservice.ts
    public intercept() {
        const token = Cookies.get('token');
        this.Language = Cookies.get('language');
        this.headers = new HttpHeaders(
          {
            Authorization: 'JWT ' + token,
            'Accept-Language': this.Language
          }
        );
        return this.headers;
      }
    
      //other file 
       this.headers = this.headersService.intercept();
    

    But it doesn´t work on the other file where i am doing the fetch, so i remove the service and put the headers directly inside the function and it works! like this:

    const token = Cookies.get('token');
    const language = Cookies.get('language');
    const headers = { 
     Authorization: 'JWT ' + token,
    'Accept-Language': language
    }
    const fetchParams = { method: 'GET',
               headers: (headers) };
    fetch(url, fetchParams)
    
    0 讨论(0)
提交回复
热议问题