简体   繁体   English

AngularJS,ngResource-意外的令牌错误

[英]AngularJS, ngResource - Unexpected token Error

EDIT: 编辑:

I got the mistake but no solution. 我弄错了,但没有解决办法。 It seems that 'isEditable', 'isOnline' and 'isRecycled' are not sent as booleans but as strings. 似乎“ isEditable”,“ isOnline”和“ isRecycled”不是作为布尔值而是作为字符串发送。 Therefore my Validaton did not pass and the Error-Response was not valid JSON. 因此,我的Validaton没有通过,并且错误响应不是有效的JSON。

But why does the .save() call sents the booleans as string? 但是,为什么.save()调用将布尔值发送为字符串?


Original Post 原始帖子

Can anyone tell me why following function throws 谁能告诉我为什么下面的函数抛出

SyntaxError: Unexpected token ' 语法错误:意外令牌'

at Object.parse (native) 在Object.parse(本机)

at fromJson ( https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.js:1072:14 ) 在fromJson( https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.js:1072:14

As my postData is logged correctly, I think there is a mistake in my resource-save. 由于正确记录了postData,因此我认为资源保存有误。 On the server side I have a simple selfmade php-framework made using Symfony2 components and the $save call of the resource adds a new row in my database and returns this row as a JSON object. 在服务器端,我有一个使用Symfony2组件制作的简单的自制php框架,资源的$ save调用在数据库中添加了新行,并将该行作为JSON对象返回。

        $scope.createStructure = function($event, createdStructure){
        $event.preventDefault();
        if(createdStructure.copy != null){
            copyId = createdStructure.copy.id;
        } else {
            copyId = 0;
        }
        postData = {
            'title': createdStructure.title,
            'id': copyId,
            'isEditable': false,
            'isOnline': false,
            'isRecycled': false
        }
        console.log(postData);
        Structure.save({}, postData, function(response){
            console.log(response);
            console.log(newStructure);
        });
    }

PHP can't tell what data type is being passed in. The POST data is seen as a string. PHP无法确定传入的数据类型。POST数据被视为字符串。 Check this previous answer out. 检查此先前的答案 I believe it covers the issue you are having. 我相信它涵盖了您遇到的问题。

PHP also has FILTER_VALIDATE_BOOLEAN , which is also covered in the answer I linked. PHP也有FILTER_VALIDATE_BOOLEAN ,我链接的答案中也有介绍。 I think it'll help you. 我想会帮你的。

For example: 例如:

$isEditable= filter_var ($_POST['isEditable'], FILTER_VALIDATE_BOOLEAN);

That should make $isEditable a boolean. 那应该使$isEditable一个布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM