我使用 create-react-app 来构建我的 React 应用程序。这个应用程序在另一个 API (elasticsearch) 上执行 POST 调用,该 API 托管在不同的服务器上(不是我拥有/管理的)。因此,一旦用户在表单中输入数据,onSubmit 基本上会调用进行调用的 getResponse() 方法。初始化客户端:
let client = new elasticsearch.Client({
host: "https://{cred.user}:{cred.pass}@@servername.domain:11121",
log: "trace",
});
Run Code Online (Sandbox Code Playgroud)
API查询:
getResponse = () => {
client
.search({
index: 'custom_index_1',
body: {
query: {
match: {"data": this.state.data}
},
}
},function(error, response, status) {
if (error) {
const errorMessage= {error};
console.log(errorMessage);
}
else {
this.setState({results: response.hits.hits});
console.log(this.state.results);
}
});
}
Run Code Online (Sandbox Code Playgroud)
但我收到 CORS 错误如下:
Failed to load resource: the server responded with a status of 403 (Forbidden) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在使用 highcharts ( https://api.highcharts.com/highcharts/ ) 的react js 中构建饼图,它只接受以下格式的饼图数据(或者我可能错了): Sample Fiddle here : https://jsfiddle.net/react_user1/e9cbsrdL/1/
data: [
{name: 'abc', y: 10},
{name: 'def', y: 90}
]
Run Code Online (Sandbox Code Playgroud)
我从 API 获得的数据如下所示:
const counts:[
{
"id": "all",
"type": "all",
"count": 1403
},
{
"id": "bad",
"type": "bad",
"count": 0
},
{
"id": "failed",
"category": false,
"type": "failed",
"count": 58
},
{
"id": "changed",
"category": true,
"type": "changed",
"count": 123
}
Run Code Online (Sandbox Code Playgroud)
所以我试图在这里实现三件事:
1. Remove the first {}, with the "id": "all"
2. Rename the key: "id" …Run Code Online (Sandbox Code Playgroud)