我有一個工作的罰款守則,突然出現在角說法錯誤:角意外的標記{
SyntaxError: Unexpected token {
at Object.parse (native)
at fromJson (http://localhost/public_html/faculte/js/angular.js:1250:14)
at defaultHttpResponseTransform (http://localhost/public_html/faculte/js/angular.js:9371:16)
at http://localhost/public_html/faculte/js/angular.js:9462:12
at forEach (http://localhost/public_html/faculte/js/angular.js:336:20)
at transformData (http://localhost/public_html/faculte/js/angular.js:9461:3)
at transformResponse (http://localhost/public_html/faculte/js/angular.js:10241:23)
at processQueue (http://localhost/public_html/faculte/js/angular.js:14634:28)
at http://localhost/public_html/faculte/js/angular.js:14650:27
at Scope.parent.$get.Scope.$eval (http://localhost/public_html/faculte/js/angular.js:15878:28)
在我的代碼我送一個HTTP請求到PHP和這裏的JS代碼:
main.submitNewChap = function(){
var data = main.newChap;
data.function = 'submitNewChap';
$http({
url: "ajax-functions.php",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $httpParamSerializerJQLike(data)
}).success(function(res) {
switch(res.msg){
case 1:
$translate(['success','newChapSuccess']).then(function(t){
toastr.success(t.newChapSuccess,t.success);
});
main.newChap = {};
main.data.chaps.push(res.item);
break;
case 2:
$translate(['error','missingData']).then(function (t) {
toastr.error(t.missingData,t.error);
});
break;
case 3:
$translate(['error','notAllowed']).then(function (t) {
toastr.error(t.notAllowed,t.error);
});
break;
}
}).error(function(res) {
$translate(['error','generalError']).then(function (t) {
toastr.error(t.generalError,t.error);
});
});
}
而且PHP函數:
function submitNewChap($data){
$res = 0;
$item = array();
if(userData('role') == 'admin'){
if($data['title'] != ''){
$item = R::dispense('chap');
$item->title = $data['title'];
$item->date = time();
$id = R::store($item);
$res = 1;
$item = array(
'id' => $id,
'title' => $item->title,
'date' => $item->date,
'courses' => array()
);
}else{
$res = 2;
}
}else{
$hack = R::dispense('hack');
$hack->text = "Trying to hack and adding Chap";
R::store($hack);
$res = 3;
}
return array(
'msg' => $res,
'item' => $item
);
/*
1: ok
2: missing data
3: not allowed
*/
}
,這是我的Ajax代碼的functions.php:
include 'functions.php';
if(isset($_POST['function'])){
switch ($_POST['function']) {
case 'submitNewChap':
echo json_encode(submitNewChap($_POST));
default:
break;
}
}
我發現在我的代碼沒有錯誤(至少我認爲),我到處找,但我沒有找到網絡上的任何解決方案,有什麼問題 ?
服務器響應的問題。你可以發佈嗎? – dfsq
我怎樣才能得到它? – Burawi
這似乎是一個事實,即不必返回JSON,你正在返回的PHP代碼,這是不JSON編碼的問題,因此分析是錯誤的。你也不會把標題放在php中。如果你這樣做,請用代碼告訴我們。 –