Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.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 with React读取字符串时有什么问题?_Javascript_Reactjs_Fetch - Fatal编程技术网

Javascript 使用fetch with React读取字符串时有什么问题?

Javascript 使用fetch with React读取字符串时有什么问题?,javascript,reactjs,fetch,Javascript,Reactjs,Fetch,这个方法有什么问题?问题是“this.context”变量仅在这个fetcharrow函数中作为字符串可见。除此之外,它是一个对象。为什么?我如何从这个对象中获取字符串(在其他类方法中)?(服务器(JEE6/Tomcat)发送字符串数据,而不是json对象) 只是告诉大家,解决方案只是更改变量的名称。这项工作: componentDidMount() { fetch("GetContext") //.then(res => res.json()) .then

这个方法有什么问题?问题是“this.context”变量仅在这个fetcharrow函数中作为字符串可见。除此之外,它是一个对象。为什么?我如何从这个对象中获取字符串(在其他类方法中)?(服务器(JEE6/Tomcat)发送字符串数据,而不是json对象)


只是告诉大家,解决方案只是更改变量的名称。这项工作:

componentDidMount() {
    fetch("GetContext")
      //.then(res => res.json())
      .then( res => res.text ())
      .then(
        (text) => {
          this.serverContext = text;
          this.initBackground (text);              
        },
        (error) => {
          console.log ("context init error "+error);
        }
    )        
}

能否显示组件的代码?在向服务器发送请求后,this.context超出范围,将
this.context
在方法外部声明为
常量
,您应该在
内部获取原始值,然后
。您对
context
的使用非常模糊:您是想使用自己的名为
context
的变量,还是尝试使用React
context
?他们可能会在这里相撞。谢谢你的回答。似乎只有更改变量的名称才能解决问题。也许“this.context”在javascript或react中是保留字。正确,它在react中是保留的:
componentDidMount() {
    fetch("GetContext")
      //.then(res => res.json())
      .then( res => res.text ())
      .then(
        (text) => {
          this.serverContext = text;
          this.initBackground (text);              
        },
        (error) => {
          console.log ("context init error "+error);
        }
    )        
}