【发布时间】:2016-11-03 06:26:40
【问题描述】:
我想“有条件地”发送一个键值。
基本上,它目前的方式仍然发送字段KEY_TEST,无论它是否包含值。
如果没有价值,我想根本不发送该字段。所以它会发送{"text":"blah","KEY_TEST":""}或{"text":"blah","KEY_TEST":1}
换句话说……
如果this.props.photo.info[0] 中有值,则 fetch 应该发送:
{"text":"blah","KEY_TEST":1}
如果this.props.photo.info[0] 中没有值,则 fetch 应该发送:
{"text":"blah"}
这是我的完整代码:
editorUpdate: function(e) {
e.preventDefault();
return fetch(URL {
method: 'PUT',
body: JSON.stringify({
"info": [{
"text": this.state.value.toString('html'),
"KEY_TEST": this.props.photo && this.props.photo.info && this.props.photo.info[0] && this.props.photo.info[0].pk || '',
}]
})
})
.then(function(res){ return res.json(); })
.then(function(data){ alert( JSON.stringify( data ) ) })
},
【问题讨论】:
-
为什么在添加到正文之前不检查?
-
请给我一个例子。我不知道它可能存在于
JSON.stringify -
试试下面的代码