Javascript 角形意外标记{

Javascript 角形意外标记{,javascript,php,angularjs,Javascript,Php,Angularjs,我有一个工作正常的代码,突然出现了一个错误,说: 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

我有一个工作正常的代码,突然出现了一个错误,说:

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)
在我的代码中,我向PHP发送了一个http请求,下面是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;
}
}

我在代码中没有发现任何错误(至少我认为是这样),我到处搜索,但在web上没有找到任何解决方案,问题出在哪里?

您应该能够在开发工具控制台中右键单击,然后单击“启用XMLHttpRequest日志记录”

一旦启用,您将在控制台中看到XHR(ajax)请求,并可以单击它们将您带到资源面板,在那里您将能够看到请求的内容/响应

如果您的请求中存在php错误,您可以在那里看到错误详细信息


这不是答案。这应该是一个评论。但是正如你所看到的,我是这里的新手。

服务器响应的问题。您可以发布它吗?这似乎是一个问题,因为您没有返回JSON,您返回的是php代码,它不是JSON编码的,因此解析错误。此外,在php中也没有输入标题。如果你这么做了,请用代码告诉我们。我编辑了这条消息,即使这应该是一条评论,我也将其标记为最佳答案!这对我解决问题帮助很大!我错过了“休息”在我的php中。。。谢谢lot@Burawi很高兴这对你有帮助。