通过Websocket连接API时出现错误。 失败:WebSocket握手期间出错:意外的响应代码:405 使用axios post方法成功获取数据。
import React, {Component} from 'react';
import axios from 'axios';
//using websocket to get the live data
var exampleSocket = new
WebSocket('ws://dev.eastus.azure.com/Thingworx/Things
/OtONE_BusThing/Services/chelg?Key=be659');
exampleSocket.onopen = function (event) {
console.log('connection is open');
};
exampleSocket.onmessage = function (event) {
console.log('event.data', event.data);
}
exampleSocket.onerror = function(evt) {
console.log('ws normal error: ' + evt.type);
};
class App extends Component {
state = {
persons: []
}
componentDidMount() {
console.log('DidMount');
setInterval(() => {
axios.post('http://dev.eastus.azure.com/Thingworx/
Things/OtONE_BusThing/Services/chelg?
Key=be659',
{"content-type": "application/x-www-form-urlencoded",
"accept": "application/json"
})
.then(res => {
console.log('res.data', res.data);
const persons = res.data;
this.setState({ persons });
})
.catch(error => {
console.log(error);
})
}, 2000)
}
render() {
return (
<div>App </div>
)
}
};
export default App;
我需要从api websocket调用中获取数据,尽管在axios帮助下,数据已成功加载到控制台中。