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:
- How to Solve JS error: Unexpected end of JSON input,Unexpected token u in JSON at position 0
- [Solved] JSON.parse() Error: Unexpected end of JSON input
- [Solved] Uncaught SyntaxError: Unexpected token ‘<‘
- Error in created hook: “SyntaxError: Unexpected token u in JSON at position 0
- [Solved] Error in created hook: “SyntaxError: Unexpected token o in JSON at position 1“
- [Solved] JS Error: Uncaught SyntaxError: Illegal return statement
- [Solved] Uncaught SyntaxError: Cannot use import statement outside a module
- vue Error: Uncaught SyntaxError: Invalid shorthand property initializer
- [Solved] Uncaught SyntaxError: Invalid Unicode escape sequence
- [Solved] Uncaught SyntaxError: The requested module does not provide an export named
- node.js yarn Error: SyntaxError: Unexpected string [How to Solve]
- [Solved] Webpack compile error: SyntaxError: Unexpected token =
- [Solved] React Startup Error at the First time :SyntaxError: Unexpected token
- JS async await Error: Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
- If the request parameter is formdata, use the Ajax operation
- Difference between contenttype and datatype in Ajax request of jquery
- [Solved] TypeError: Converting circular structure to JSON – JSON.stringify
- [Solved] it only responds to error and does not enter success after AJAX is successfully processed
- The solution that needs alert to execute after using ajax