Uncaught SyntaxError: Unexpected token o
Reprinted from: http://codego.net/600533/
I’ve seen all so and Google, but I’ve also noticed that help, I don’t know how to continue. I have an array that I return from echo JSON_ Encode ()
looks like this:
["","","","","",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
But I keep receiving unexpected token o at object. Parse (native)
when I try JSON. Parse() and unexpected token o at function. Parse (native)
when jQuery is alternative. But when I just connect it to $scope
I can print it out on the page, so what’s wrong with me and how can I fix it?This is my controller
functionmyController($scope,memberFactory){response=memberFactory.getMonth("2013-08-0106:30:00");//$scope.response=response;varmonthDays=$.parseJSON(response);vardates=[];for(vari=0;i<monthDays.length;i++){if(i%7==0)dates.push([]);dates[dates.length-1].push(monthDays[i]);}$scope.dates=dates;//}
This is how I serve:
obj.getMonth=function(date){varmonth=$q.defer();$http.get('getMonth.php?date='+date).success(function(data,status,headers,config){month.resolve(data);});returnmonth.promise;}
This is:
<?php$daysOfMonth=["","","","","",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];echojson_encode($daysOfMonth);?>
I’ve tried to return the object I got from type of
so later I tried var monthdays = array. Prototype. Slice. Call (response)
and var monthdays = $. Map (response, function (value, key) {return value;})
the answer here suggests codego.net. I also try to JSON. Stringify()
myself, but I just get {}
as a result. I really need to update in the right direction. I believe this may be a problem. $q.defer()
I update my getmonth method as follows:
obj.getMonth=function(date){varmonth=$q.defer();$http.get('getMonth.php?date='+date).success(function(data,status,headers,config){month.resolve(data);console.log("data"+data[0]);console.log("resolved"+month.promise[0]);});returnmonth.promise;}
Now I get 1
as console.log ("data" + data [0])
as expected, but I got console.log (month)
I get [object object]
as console.log (month. Promise)
I get [object]
and console.log (month. Promise [0])
I got undefined
address of this article: codego.net/600533/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -. If it is parsed again, it will first execute a toString cast, so you are like parsing "[object object]
this also explains unexpected token o
2. This is solved by replacing @ cuongle here
response=memberFactory.getMonth("2013-08-0106:30:00");varmonthDays=$.parseJSON(response);
There are:
response=memberFactory.getMonth("2013-08-0106:30:00");response.then(function(monthDays){console.log("monthDays:"+monthDays+"!!!");vardates=[];for(vari=0;i<monthDays.length;i++){if(i%7==0)dates.push([]);dates[dates.length-1].push(monthDays[i]);}$scope.dates=dates;});
3. Please take a look at the request and response of HTTP module in a chapter. If the JSON response is detected, deserialize the JSON parser. Because it has been parsed as a JSON object, if you analyze it again, you will get this error. Here's a simple test:
response='{"a":"a","b":"b"}';varobj=$.parseJSON(response);console.log(obj);//Object{a:"a",b:"b"}$.parseJSON(obj)//UncaughtSyntaxError:Unexpectedtokeno
Article title: what causes "uncapped syntax error: unexpected token o" and $parsejson() and JSON. Parse()
Similar Posts:
- How to Solve Error: Unexpected token o in JSON at position 1
- Unexpected token o in JSON at position 1 at JSON.parse (
) SyntaxError: Unexp… - Unexpected token ‘ in JSON at position 2 at JSON.parse
- When parsing background JSON data with Ajax: unexpected token o in JSON at position 1
- [Solved] Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse
- [Solved] Vue Template Error: You may have an infinite update loop in a component render function,vue
- [Solved] Jquery each error: Uncaught TypeError: Cannot use ‘in’ operator to search for
- [Solved] Vue Axios error: TypeError: Cannot set property ‘tableData‘ of undefined
- Json Uncaught SyntaxError: Unexpected token ‘ Uncaught SyntaxError: Unexpected number
- Ajax “SyntaxError: missing ; before statement” [How to Solve]