是什么导致了$.parseJSON()和JSON.parse()的“Uncaught SyntaxError:意外令牌o
What is causing “Uncaught SyntaxError: Unexpected token o” with $.parseJSON() and JSON.parse()
我已经看遍了SO和谷歌,但注意到这有帮助,我不确定如何继续。我有一个数组,我使用echo json_encode()
从php页面返回,它看起来像这样:
[" "," "," "," "," ",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]
但当我尝试使用JSON.parse()时,我一直得到Unexpected token o at Object.parse (native)
,而当我使用JQuery变量时,我得到Unexpected token o at Function.parse (native)
。
但当我把它附加到$scope
上时,我可以在页面上使用打印出来,那么我做错了什么,我该如何纠正?
这是我的控制器
function myController($scope, memberFactory) {
response = memberFactory.getMonth("2013-08-01 06:30:00");
//$scope.response = response;
var monthDays = $.parseJSON(response);
var dates = [];
for (var i = 0; i < monthDays.length; i++) {
if (i % 7 == 0) dates.push([]);
dates[dates.length - 1].push(monthDays[i]);
}
$scope.dates = dates;
//
}
这是我的服务方式:
obj.getMonth = function (date) {
var month = $q.defer();
$http.get('getMonth.php?date=' + date)
.success(function (data, status, headers, config) {
month.resolve(data);
});
return month.promise;
}
这是我的php:
<?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];
echo json_encode($daysOfMonth); ?>
我尝试过的东西
如果我返回typeof
,我会得到Object,所以我已经尝试了var monthDays = Array.prototype.slice.call(response)
和var monthDays = $.map(response, function (value, key) { return value; });
,正如这里的一些答案所建议的那样,我也尝试过JSON.stringify()
,但我只得到了{}
作为
我真的很沮丧,真的需要有人给我指正确的方向
更新
我相信这可能是使用$q.defer()
的问题,因为我更新了getMonth方法如下:
obj.getMonth = function (date) {
var month = $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]);
});
return month.promise;
}
现在我得到了console.log("data " + data[0]);
的1
,正如预期的那样,但我得到console.log(month);
的[object Object]
的console.log(month.promise)
的[object Object]
和console.log(month.promise[0]);
的undefined
response
已经解析,不需要再次解析。
如果您再次解析它,它将首先执行toString强制转换,因此您正在解析
"[object Object]"
这解释了CCD_ 20。
请查看$http模块的转换请求和响应一章。
如果检测到JSON响应,请使用JSON解析器对其进行反序列化。
由于它已经被解析为JSON对象,如果您再次解析它,您将得到该错误。
这里有一个简单的测试:
response = '{"a": "a","b": "b"}';
var obj = $.parseJSON(response);
console.log(obj); //Object {a: "a", b: "b"}
$.parseJSON(obj) //Uncaught SyntaxError: Unexpected token o
这是在@CuongLe的帮助下通过替换解决的
response = memberFactory.getMonth("2013-08-01 06:30:00");
var monthDays = $.parseJSON(response);
带有:
response = memberFactory.getMonth("2013-08-01 06:30:00");
response.then(
function (monthDays) {
console.log("monthDays : " + monthDays + " !!!");
var dates = [];
for (var i = 0; i < monthDays.length; i++) {
if (i % 7 == 0) dates.push([]);
dates[dates.length - 1].push(monthDays[i]);
}
$scope.dates = dates;
});
如果发送了标头:
Content-Type: text/json
则不需要调用CCD_ 21。例如:
在PHP中,我会这样设置标题:
<?php
header("Content-Type: text/json");
echo json_encode(array("someKey" => "someValue"));
在Javascript中,我会有这样的成功函数:
success: function(response) {
// This will output "someValue" to the console.
// Note the lack of the parseJSON() call. parseJSON() is called for us because the header was sent as text/json
console.log(response.someKey);
}
- Uncaught SyntaxError:尝试读取json数据时出现意外数字
- 为什么JSON.parse()在尝试解析一个简单的JSON对象时抛出Uncaught SyntaxError:意外的令牌
- PHP调用脚本函数,返回值为Uncaught SyntaxError:意外的令牌ILLEGAL
- Uncaught SyntaxError:在剑道网格中动态生成列时出现意外数字
- Snap.svg and javascript throws Uncaught SyntaxError
- 我在这里得到了Uncaught SyntaxError:意外的令牌<.为什么会这样
- 是什么导致了$.parseJSON()和JSON.parse()的“Uncaught SyntaxError:意外令牌o
- jQuery AJAX POST请求抛出Uncaught SyntaxError:意外标识符
- 为什么我会得到Uncaught SyntaxError:意外的令牌非法
- ajaxt weird error: Uncaught SyntaxError: Unexpected token if
- index.php:13 Uncaught SyntaxError: Unexpected token: Ci中的错误消
- 试图调用JS函数并获得Uncaught SyntaxError: Unexpected identifier
- Uncaught SyntaxError: ajax jquery的意外标识符
- 接收错误"Uncaught SyntaxError: Unexpected token "用Shin
- React JS应用程序- Uncaught SyntaxError: Unexpected token <
- 尝试使用标记的模板字符串给出'Uncaught SyntaxError: Unexpected token
- Javascript for循环返回“Uncaught SyntaxError: Unexpected Token <&
- 在尝试动态删除表行时,得到Uncaught SyntaxError: Unexpected token}
- Javascript和Chrome调试器:"Uncaught SyntaxError: Unexpected
- AngularJS Flickr API 'Uncaught SyntaxError: Unexpected t