Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.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/4/json/13.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/1/dart/3.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_Json_Wordpress_Reactjs_Wordpress Gutenberg - Fatal编程技术网

Javascript 反应:如何防止数据属性中的转义

Javascript 反应:如何防止数据属性中的转义,javascript,json,wordpress,reactjs,wordpress-gutenberg,Javascript,Json,Wordpress,Reactjs,Wordpress Gutenberg,我有一个类似于{“id”:“xyz”,“height”:1024,“width”:1024}的JSON,我希望在数据属性中包含它,如: <div data-command='{"id":"xyz","height":1024,"width":1024}'></div> 有人知道我如何在不使用“转义”的情况下获取JSON吗 如果不可能,我如何将其转换回javascript,以便在以后使用JSON.parse?类似的场景中使用“DangerouslySetinerHTML”

我有一个类似于{“id”:“xyz”,“height”:1024,“width”:1024}的JSON,我希望在数据属性中包含它,如:

<div data-command='{"id":"xyz","height":1024,"width":1024}'></div>
有人知道我如何在不使用“转义”的情况下获取JSON吗


如果不可能,我如何将其转换回javascript,以便在以后使用JSON.parse?

类似的场景中使用“DangerouslySetinerHTML”属性

createMarkup() {
    return {__html: `<div data-command='{"id":"xyz","height":1024,"width":1024}'></div>`};
}

render() {
    return (
        <div dangerouslySetInnerHTML={this.createMarkup()}>
        </div>
    );
}
createMarkup(){
返回{uuu html:``};
}
render(){
返回(
);
}

对于这种情况,“危险的LysetinerHTML”属性正是使用的

createMarkup() {
    return {__html: `<div data-command='{"id":"xyz","height":1024,"width":1024}'></div>`};
}

render() {
    return (
        <div dangerouslySetInnerHTML={this.createMarkup()}>
        </div>
    );
}
createMarkup(){
返回{uuu html:``};
}
render(){
返回(
);
}

你不能把
{“id”:“xyz”,“height”:1024,“width”:1024}
放在应用程序状态下,使用索引或其他引用作为数据命令吗?或者使用id=“xyz”?您好,它在Gutenberg wordpress块中,所以在管理部分使用react,但是当为客户端呈现时,react不再使用,只剩下属性(在管理部分,我确实使用了state,由于它没有问题)它必须被字符串化为有效的JSON吗?否则你可以尝试使用类似这样的东西:
{“data command”:“{id:'xyz',height:1024,width:1024}”),null)
。是的,它需要用JSON解析回来。解析你不能把
{“id”:“xyz”,“height:1024”,width放回去吗“:1024}
在应用程序状态下,使用索引或其他引用作为数据命令?或使用id=“xyz”?您好,它在Gutenberg wordpress块中,因此在管理部分使用react,但在为客户端呈现时,react不再使用,只剩下属性(在管理部分,我确实使用了state,由于它没有问题)是否必须将其字符串化为有效的JSON?否则您可以尝试使用类似以下内容:
{“data command”:“{id:'xyz',height:1024,width:1024}”),null)
。是的,它需要通过JSON进行解析。parsethis可以工作,但是我不能引入额外的父级,我猜这是无法完成的,因为它设置了内部Html请注意,这是不安全的,可能会导致XSS攻击!我建议避开
危险的SetinenerHtml
。通常还有另一种解决方案可以做到这一点sn不涉及不安全的操作。这是可行的,但是我不能引入额外的父级,我想这是无法做到的,因为它设置了InnerHtml请注意,这是不安全的,可能会导致XSS攻击!我建议避开
危险的LysetinerHtml
。通常还有另一种解决方案不涉及不安全的操作。
createMarkup() {
    return {__html: `<div data-command='{"id":"xyz","height":1024,"width":1024}'></div>`};
}

render() {
    return (
        <div dangerouslySetInnerHTML={this.createMarkup()}>
        </div>
    );
}