SyntaxError: Unexpected token o in JSON at position 1
The JSON you posted looks fine, however in your code, it is most likely not a JSON string anymore, but already a JavaScript object. This means, no more parsing is necessary.
You can test this yourself, e.g. in Chrome's console:
new Object().toString()
// "[object Object]"
JSON.parse(new Object())
// Uncaught SyntaxError: Unexpected token o in JSON at position 1
JSON.parse("[object Object]")
// Uncaught SyntaxError: Unexpected token o in JSON at position 1
JSON.parse()
converts the input into a string. The toString()
method of JavaScript objects by default returns [object Object]
, resulting in the observed behavior.
Try the following instead:
var newData = userData.data.userList;
how to fix Uncaught SyntaxError: Unexpected token o in JSON at position 1
No need to use parse again , its already json
$.ajax({
url: "/url/url.ajax?length=100000&startDate=2018-07-01",
method: "get",
dataType: "json",
success: function (jdata) {
var jsonData=jdata.data;
}
});
$.ajax({
url: "https://jsonplaceholder.typicode.com/posts",
method: "get",
dataType: "json",
success: function (jdata) {
var jsonData=jdata
console.log(jsonData)
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Facing an Uncaught SyntaxError: Unexpected token o in JSON at position 1
first line of showNotes
function (line 117 on your screen shot), let Notes = …
"Notes" should have a lower "n"
Uncaught SyntaxError: Unexpected token :
I have just solved the problem. There was something causing problems with a standard Request call, so this is the code I used instead:
vote.each(function(element){
element.addEvent('submit', function(e){
e.stop();
new Request.JSON({
url : e.target.action,
onRequest : function(){
spinner.show();
},
onComplete : function(){
spinner.hide();
},
onSuccess : function(resp){
var j = resp;
if (!j) return false;
var restaurant = element.getParent('.restaurant');
restaurant.getElements('.votes')[0].set('html', j.votes + " vote(s)");
$$('#restaurants .restaurant').pop().set('html', "Total Votes: " + j.totalvotes);
buildRestaurantGraphs();
}
}).send(this);
});
});
If anyone knows why the standard Request object was giving me problems I would love to know.
How to fix Unexpected token o in JSON at position 1 using JavaScript?
add json()
let data = res.json();
Uncaught SyntaxError: Unexpected token o in JSON at position 1 error
You are parsing the data that is already parsed,
try using "data" directly
$("#search").change(function () {
$("html, body").animate({ scrollTop: $(".calendar").offset().top }, 1500);
var selectedEvent = $("#search").val();
$.getJSON('events.json', function (data) {
render(selectedEvent, data);
});
});
Related Topics
How to Avoid 'Cannot Read Property of Undefined' Errors
Response to Preflight Request Doesn't Pass Access Control Check
How to Convert String to Datetime With Format Specification in JavaScript
Jspdf Can't Get Any Styling to Work
Jquery: How to Check If the Element Has Certain CSS Class/Style
Jquery UI Autocomplete Positioning Wrong
Web Worker Settings for Chrome
Uncaught Referenceerror: Function Is Not Defined with Onclick
Ecmascript Template Literals Like 'Some ${String}' Are Not Working
Best Way to Detect When a User Leaves a Web Page
Whats the Best Way to Update an Object in an Array in Reactjs
How to Use the Spacing Utility Classes in Bootstrap
How to Concatenate and Minify Multiple CSS and JavaScript Files with Grunt.Js (0.3.X)
Dynamically Expand Height of Input Type "Text" Based on Number of Characters Typed into Field