When using websocket to push the JSON data to the front end, websocket needs to convert the JSON data into a string for transmission. In the front end, JavaScript is used to restore the text to a JSON object. Generally, the JSON. Paser () method is used to complete this work. However, in the actual transmission, there is a problem. The JSON. Parse() method always reports an error
Uncaught SyntaxError: Unexpected token ‘ in JSON at position 1
at JSON.parse (< anonymous>)
at WebSocket.ws.onmessage ((index):28)
After checking and comparing, it is found that in the process of transferring string by websocket, double quotation marks in string are replaced by single quotation marks, while JSON. Parse() method requires double quotation marks in string. After finding this reason, it is easy to solve it. Before parse conversion, single quotation marks in string are replaced by double quotation marks.
ws.onmessage = function (evt) {
var a = evt.data.replace(/'/g, '"');
var obj = JSON.parse(a);
.....
Read More:
- 【ipfs-api】npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.
- HTML method IE8 reports an error, IE8 jQuery Ajax obtains static resources reports an error, typeerror denies access
- JSON data format net.sf.json .JSONException: A JSONObject text must begin with ‘{‘ at character 1 of Error:(f…
- Mybatis uses step-by-step lazy loading to cause abnormal JSON conversion. The interface 500 reports an error
- 209151; org.json.JSONException A JSONObject text must begin with'{‘at character 1 of {
- Unit Android converts c# class to JSON file and reports an error
- Redis reports an error when integrating springsecurity: could not read JSON: unrecognized field “enabled”“
- Vue agent reports an Error 404 nginx configuration method
- WebSocket failed: Error during WebSocket handshake: Unexpected response code: 400
- JSON parse error: invalid UTF-8 solution series
- JSON parse e rror: Invalid UTF-8 middle byte 0x3f;
- The Vue parent component uses ref to call the sub component method and reports an error
- Spock + powermock simulation static method reports an error java.lang.classcastexception
- Deployment project websocket failed: error during websocket Handshake: unexpected response code: 400
- Hadoop reports an error. Cannot access scala.serializable and python MapReduce reports an error
- The echots in Vue reports an error. After obtaining the DOM element, the chart can be displayed. The console still reports an error
- PHP reports “parse error: syntax error, unexpected T”_ The solution of “variable”
- Lamdba in the studio part reports an error. Observe lamdba reports an error but can run
- The file server reports an error of 413, and the file uploaded by nginx reports an error of 413 request entity too large
- main.cpp : (. Text + 0xd06): undefined reference to XX method | simple record