render: function(){
console.log(this.state.search_results);
var renderedSearchResults = this.state.search_results.map((result) => {
console.log 打印:
[{"id": "testGroup2"}, {"id": "testGroup77777"}, {"id": "testGroup1"}, {"id": "testGroup3"}]
数据通过以下方式获取:
$.ajax({
url: this.props.routes.search,
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(searchObj),
success: function(data){
console.log(data);
this.setState({search_results:data});
}.bind(this),
error: function(xhr, status,err){
console.error("/api/response", status, err.toString());
}.bind(this)
});```
通过:
def post(self):
"""
Makes a request to search for a specific
searchtype: Group
searchstring: ""
requestedfields: []
"""
search_type = self.json_data.get('searchtype', 'Group')
search_string = self.json_data.get('searchstring', '')
requestedfields = self.json_data.get('requestedfields', ['id'])
search_model = {
'Group': Group(),
'User': User()
}[search_type]
search_fields = {
'Group': ['id', 'tags'],
'User': ['username']
}[search_type]
# check if requestedfields contains valid fields
for field in requestedfields:
if field == 'id':
continue
value = search_model.default().get(field, None)
if value is None:
return self.set_status(400, "Model {0} does not have requested field {1}".format(search_type, field))
try:
search_results = search_model.search_items(search_string, search_fields, requestedfields)
except err:
return self.set_status(400, "Something went wrong")
self.set_status(200, "Success")
return self.write(tornado.escape.json_encode(search_results))
我真的很困惑 this.state.search_results
不是一个可以迭代的数组,有人能看到发生了什么吗?
我尝试在 success 函数中使用 console.log(Object.prototype.toString.call(data));
,我得到:
[object String]
最佳答案
在执行 ajax 请求时,尝试以显式方式设置 json
数据类型:
$.ajax({
dataType: 'json',
//...
});
很可能用于通过 jQuery 检测数据类型的Intelligent Guess
给出了错误的结果。
关于javascript - this.state.search_results.map 不是一个函数,而是一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36142143/