To record, the Ajax request is as follows:
let xhr = new XMLHttpRequest();
xhr.open("get","https://music.kele8.cn/search");
xhr.send();
xhr.onreadystatechange = function () {
// Determine when the Ajax status code is 4
if (xhr.status==200) {
// Get the response data from the server side
let ans = JSON.parse(xhr.responseText);
console.log(ans);
}
else {
console.log("Request failed");
}
}
If you ensure that the data returned in the background is correct, the get request can be opened in the browser address bar to see the data in JSON format, but the console reports an error and the data is also obtained, it may be that you lack a judgment
Look at the following code:
let xhr = new XMLHttpRequest();
xhr.open("get", "https://music.kele8.cn/search?keywords="+str);
xhr.send();
xhr.onreadystatechange = function () {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
let arr = xhr.responseText;
let ctype = xhr.getAllResponseHeaders("content-type");
if(ctype.indexOf("json") > -1) {
let res = JSON.parse(arr);
console.log(res);
}
else {
console.log(arr);
}
}
else {
reject('Request failed');
}
}
}
It seems that you need to judge XHR in the onreadystatechange event If the readyState attribute is not judged, an error will be reported by JSON
Read More:
- Interface request error 504 gateway time out [How to Solve]
- How to Solve JS error: Unexpected end of JSON input,Unexpected token u in JSON at position 0
- Difference between contenttype and datatype in Ajax request of jquery
- Error in created hook: “SyntaxError: Unexpected token u in JSON at position 0
- Localstorage sets the expiration time.
- [Solved] JSON.parse() Error: Unexpected end of JSON input
- If the request parameter is formdata, use the Ajax operation
- @requestbody: How to Use or Not Use
- Json.parse: All Error & How to Solve Them
- Vue2.0: How to Use vue3 api to encapsulate Axios
- [Solved] express-jwt Error: Error: jwt is not a function
- [Solved] it only responds to error and does not enter success after AJAX is successfully processed
- [Solved] Pdfjs Preview PDF error: formaterror: bad fcheck in flat stream: 120, 239
- [Solved] Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension
- How to Solve Converting circular structure to JSON‘ Error
- Vue a page is mounted to send multiple requests at the same time, and the loading is processed uniformly
- [Solved] ERROR Error: Cannot find module ‘vue-loader-v16/package.json‘
- [Solved] node.js Upload Files Error: Multipart: boundary not found multer
- Node Error: Error: Multipart: Boundary not found [How to Solve]