. Advertisement .
..3..
. Advertisement .
..4..
Have a good day, guys!
I’m facing up with the error: ”unexpected end of json input”, but up to now I haven’t solved it.
This is detailed description of my program:
<html><head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var jsondata = [''];
var jsonarray = JSON.parse(jsondata);
$.each(jsonarray, function(i, field){
$("div").append(field + " ");
});
});
});
</script>
</head>
<body>
<button>Get data</button>
<div></div>
</body></html>
After I run it, I get a warning message:
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at HTMLButtonElement.<anonymous> (json.html:7)
at HTMLButtonElement.dispatch (jquery.min.js:2)
at HTMLButtonElement.v.handle (jquery.min.js:2)
Does anyone give me a guide to fix this error? Please write your advice below. Thanks!
The cause: After researching your program for hours, I realize that an empty string is assigned to the variable jsondata. This variable is supplied as an argument to the
JSON.parse()
method in the following line, which parses it. It is transformed to an empty string during parsing. As a result, the end of the string is reached before any possible JSON text content is parsed. Therefore, you get above warning message. So how to fix this now? Don’t worry, I will help you.Solution: Let’s follow the program which I run:
As you can see from above program, I assign the variable jsondata to the form of key-value pairs – “Site”: “Stechies”, “Code”: “Jquery”. Therefore, when using the
JSON.parse()
function to parse, it is not changed to an empty string, as a result the Uncaught SyntaxError: ”unexpected end of json input” does not exist.👇️ Read more: How To Resolve The Uncaught SyntaxError: Unexpected End Of Input Error?