<i id='6UlE1'><tr id='6UlE1'><dt id='6UlE1'><q id='6UlE1'><span id='6UlE1'><b id='6UlE1'><form id='6UlE1'><ins id='6UlE1'></ins><ul id='6UlE1'></ul><sub id='6UlE1'></sub></form><legend id='6UlE1'></legend><bdo id='6UlE1'><pre id='6UlE1'><center id='6UlE1'></center></pre></bdo></b><th id='6UlE1'></th></span></q></dt></tr></i><div id='6UlE1'><tfoot id='6UlE1'></tfoot><dl id='6UlE1'><fieldset id='6UlE1'></fieldset></dl></div>
<tfoot id='6UlE1'></tfoot>
  • <legend id='6UlE1'><style id='6UlE1'><dir id='6UlE1'><q id='6UlE1'></q></dir></style></legend>

      • <bdo id='6UlE1'></bdo><ul id='6UlE1'></ul>
    1. <small id='6UlE1'></small><noframes id='6UlE1'>

        TypeError:无法在“窗口"上执行“获取":无效值

        TypeError: Failed to execute #39;fetch#39; on #39;Window#39;: Invalid value(TypeError:无法在“窗口上执行“获取:无效值)

              <small id='t3ghN'></small><noframes id='t3ghN'>

              <i id='t3ghN'><tr id='t3ghN'><dt id='t3ghN'><q id='t3ghN'><span id='t3ghN'><b id='t3ghN'><form id='t3ghN'><ins id='t3ghN'></ins><ul id='t3ghN'></ul><sub id='t3ghN'></sub></form><legend id='t3ghN'></legend><bdo id='t3ghN'><pre id='t3ghN'><center id='t3ghN'></center></pre></bdo></b><th id='t3ghN'></th></span></q></dt></tr></i><div id='t3ghN'><tfoot id='t3ghN'></tfoot><dl id='t3ghN'><fieldset id='t3ghN'></fieldset></dl></div>
                <tbody id='t3ghN'></tbody>
              <tfoot id='t3ghN'></tfoot>
            • <legend id='t3ghN'><style id='t3ghN'><dir id='t3ghN'><q id='t3ghN'></q></dir></style></legend>
                • <bdo id='t3ghN'></bdo><ul id='t3ghN'></ul>
                • 本文介绍了TypeError:无法在“窗口"上执行“获取":无效值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试使用 fetch 从后端调用 react,没有库(例如 Axios).所以我创建了这个函数:

                  导出函数 api(url, method, body, isHeaderContentType,isRequestHeaderAuthentication,header, succesHandler, errorHandler) {常量前缀 = '链接';console.log("url:",前缀+url);常量内容类型 = isHeaderContentType ?{'内容类型':'应用程序/json',} : {};const auth = isRequestHeaderAuthentication?{授权:`Bearer ${AuthUtils.getTokenUser}`,}: {};获取(前缀+网址,{方法,标题:{...内容类型,...认证,...标题,},协议:'http:',身体,}).then(响应 => {response.json().then(json => {如果(response.ok){console.log("方法", json);如果(成功处理程序){成功处理程序(json)}} 别的 {返回 Promise.reject(json)}})}).catch(错误 => {console.log("error",`${url} ${err}`);如果(错误处理程序){错误处理程序(错误);}})

                  }并这样称呼它

                  api(`链接`,得到",空值,真的,真的,空值,响应=>{this.setState({profile:response.data})},错误=>{console.log('error', err);});

                  我在这个函数中调用了我的 api():

                  getProfileUser = () =>{if (!isUserAuthenticated()){history.push('/signin')}别的 {api(`链接`,得到",空值,真的,真的,空值,响应=>{this.setState({profile:response.data})},错误=>{console.log('error', err);});}};

                  这是我的完整组件:

                  export default class Profile extends Component {构造函数(道具){超级(道具);这个.state = {轮廓:[]}}getProfileUser = () =>{if (!isUserAuthenticated()){一些代码}别的 {api(`链接`,得到",空值,真的,真的,空值,响应=>{this.setState({profile:response.data})},错误=>{console.log('error', err);});}};组件DidMount() {this.getProfileUser();}使成为(){返回(

                  你好</div>)}

                  }

                  但是当我尝试运行它时,我收到了这样的错误

                  <块引用>

                  TypeError: 无法在 'Window' 上执行 'fetch': 无效值

                  有人知道我的代码有什么问题吗?该功能在我使用POST"方法时有效,但在我使用GET"方法时无效

                  解决方案

                  当我尝试将 Authorization 标头添加到我的 fetch 调用时,我也遇到了这种情况.在我的情况下,它是由标题字符串中的换行符引起的,即 Bearer: somelong-token.将新行更改为空格即可解决问题.

                  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, isHeaderContentType,isRequestHeaderAuthentication,header, succesHandler, errorHandler) {
                  const prefix = 'link';
                  console.log("url:",prefix+url);
                  const contentType = isHeaderContentType ? {
                      'Content-Type': 'application/json',
                  } : {};
                  const auth = isRequestHeaderAuthentication
                      ? {
                          Authorization: `Bearer ${AuthUtils.getTokenUser}`,
                      }
                      : {};
                  fetch(prefix + url, {
                      method,
                      headers: {
                          ...contentType,
                          ...auth,
                          ...header,
                  
                      },
                      protocol:'http:',
                      body,
                  })
                      .then(response => {
                          response.json().then(json => {
                              if (response.ok) {
                                  console.log("method", json);
                                  if (succesHandler) {
                                      succesHandler(json)
                                  }
                              } else {
                                  return Promise.reject(json)
                              }
                          })
                      })
                      .catch(err => {
                          console.log("error",`${url}  ${err}`);
                          if (errorHandler) {
                              errorHandler(err);
                          }
                      })
                  

                  } and call it like this

                  api(
                              `link`,
                              "GET",
                              null,
                              true,
                              true,
                              null,
                              response => {
                                  this.setState({profile:response.data})
                              },
                              err => {
                                  console.log('error', err);
                              }
                          );
                  

                  i call my api() inside this function:

                  getProfileUser = () =>{
                      if (!isUserAuthenticated()){
                          history.push('/signin')
                      }else {
                          api(
                              `link`,
                              "GET",
                              null,
                              true,
                              true,
                              null,
                              response => {
                                  this.setState({profile:response.data})
                              },
                              err => {
                                  console.log('error', err);
                              }
                          );
                      }
                  };
                  

                  this is my full component:

                  export default class Profile extends Component {
                  constructor(props) {
                      super(props);
                      this.state = {
                          profile:[]
                      }
                  
                  }
                  getProfileUser = () =>{
                      if (!isUserAuthenticated()){
                          someCode
                      }else {
                          api(
                              `link`,
                              "GET",
                              null,
                              true,
                              true,
                              null,
                              response => {
                                  this.setState({profile:response.data})
                              },
                              err => {
                                  console.log('error', err);
                              }
                          );
                      }
                  };
                  
                  componentDidMount() {
                      this.getProfileUser();
                  }
                  
                  render(){
                      return(
                          <div>
                              hello 
                          </div>
                      )
                  }
                  

                  }

                  but when i tried to run it, i got an error like this

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

                  Is there anyone who knows what's wrong with my code? The function works when I use the "POST" method, but it doesn't work when I use "GET" method

                  解决方案

                  This also happened to me when I tried to add an Authorization header to my fetch calls. In my case it was caused by a newline character in the header string, i.e. Bearer: somelong-token. Changing the new line to a space solved the problem.

                  这篇关于TypeError:无法在“窗口"上执行“获取":无效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                  <i id='gMJmI'><tr id='gMJmI'><dt id='gMJmI'><q id='gMJmI'><span id='gMJmI'><b id='gMJmI'><form id='gMJmI'><ins id='gMJmI'></ins><ul id='gMJmI'></ul><sub id='gMJmI'></sub></form><legend id='gMJmI'></legend><bdo id='gMJmI'><pre id='gMJmI'><center id='gMJmI'></center></pre></bdo></b><th id='gMJmI'></th></span></q></dt></tr></i><div id='gMJmI'><tfoot id='gMJmI'></tfoot><dl id='gMJmI'><fieldset id='gMJmI'></fieldset></dl></div>
                  <legend id='gMJmI'><style id='gMJmI'><dir id='gMJmI'><q id='gMJmI'></q></dir></style></legend>

                      <tbody id='gMJmI'></tbody>

                    <small id='gMJmI'></small><noframes id='gMJmI'>

                      • <tfoot id='gMJmI'></tfoot>

                            <bdo id='gMJmI'></bdo><ul id='gMJmI'></ul>