Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.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 编码反应中的组分_Javascript_Reactjs - Fatal编程技术网

Javascript 编码反应中的组分

Javascript 编码反应中的组分,javascript,reactjs,Javascript,Reactjs,我有一个从axios返回承诺的react函数,我需要对传递给它的等式类型字符串进行编码 const { equation } = this.state; axios.post(`${this.state.rootUrl}/integrate`, { equation }).then(some other code) 我想在将字符串表达式传递给API进行查询之前对其进行编码 我试过下面的方法,但没用 axios.post(`${this.state.rootUrl}/integrate`, {

我有一个从axios返回承诺的react函数,我需要对传递给它的等式类型字符串进行编码

const { equation } = this.state;
axios.post(`${this.state.rootUrl}/integrate`, { equation }).then(some other code)
我想在将字符串表达式传递给API进行查询之前对其进行编码

我试过下面的方法,但没用

axios.post(`${this.state.rootUrl}/integrate`, { encodeURIComponent(equation) })
我也试过:

const { equation } = this.state;
const { test } = encodeURIComponent(equation);
axios.post(`${this.state.rootUrl}/integrate`, { test }).then(some other code)
这也没用

axios.post(`${this.state.rootUrl}/integrate`, { encodeURIComponent(equation) })
下面是我尝试使用的函数的完整代码:

handleSubmit = (e) => {
    e.preventDefault();
    const { equation } = this.state;
    // const { test } = encodeURIComponent(equation);
    axios.post(`${this.state.rootUrl}/integrate`, { equation })
      .then((response) => {
        const data = response.data;
        this.setState({ data: data });
        console.log(equation);
        if (data != null) {
          this.setState({input: data.response[0]});
        }
    }
  }

使用速记似乎有问题。试着这样做:

const test = encodeURIComponent(equation); // no braces here
axios.post(`${this.state.rootUrl}/integrate`, { test }).then(some other code)


使用速记似乎有问题。试着这样做:

const test = encodeURIComponent(equation); // no braces here
axios.post(`${this.state.rootUrl}/integrate`, { test }).then(some other code)


在最初的示例中,您正在使用速记语法设置对象属性-以下两行代码是等效的:

{ equation }
{ equation: equation }
{ test }
{ test: test }
你的后两个例子做的不一样!在示例2中,您试图将速记与方法调用结合使用,但这是行不通的。在示例三中,您试图对
encodeURIComponent(equation)
的返回值进行解构,这也不起作用(它返回一个字符串)

Fawaz的第一个示例几乎可以工作,但在行为上有一个细微的区别-因为他们将变量命名为
test
,传递给Axios的对象的键也将是
test
。记住,这些是等效的:

{ equation }
{ equation: equation }
{ test }
{ test: test }
据推测,您的API需要一个名为
等式
的东西,而不是
测试
,所以这不起作用

要获得正确的行为,应确保传递的对象具有正确的键:

const test = encodeURIComponent(equation);
axios.post(`${this.state.rootUrl}/integrate`, { equation: test })

// or

axios.post(`${this.state.rootUrl}/integrate`, { equation: encodeURIComponent(equation) })

在最初的示例中,您正在使用速记语法设置对象属性-以下两行代码是等效的:

{ equation }
{ equation: equation }
{ test }
{ test: test }
你的后两个例子做的不一样!在示例2中,您试图将速记与方法调用结合使用,但这是行不通的。在示例三中,您试图对
encodeURIComponent(equation)
的返回值进行解构,这也不起作用(它返回一个字符串)

Fawaz的第一个示例几乎可以工作,但在行为上有一个细微的区别-因为他们将变量命名为
test
,传递给Axios的对象的键也将是
test
。记住,这些是等效的:

{ equation }
{ equation: equation }
{ test }
{ test: test }
据推测,您的API需要一个名为
等式
的东西,而不是
测试
,所以这不起作用

要获得正确的行为,应确保传递的对象具有正确的键:

const test = encodeURIComponent(equation);
axios.post(`${this.state.rootUrl}/integrate`, { equation: test })

// or

axios.post(`${this.state.rootUrl}/integrate`, { equation: encodeURIComponent(equation) })