角度意外令牌{

时间:2015-10-24 09:18:15

标签: 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 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;
}
}

我的代码中没有发现任何错误(至少,我认为)并且我到处搜索但我在网上找不到任何解决方案,问题是什么?

1 个答案:

答案 0 :(得分:1)

您应该可以右键单击Developer Tools控制台,然后单击"启用XMLHttpRequest日志记录"。

启用后,您将在控制台中看到XHR(ajax)请求,并且可以单击它们以转到资源面板,您可以在其中查看内容/回复您的请求。

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

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