【问题标题】:Sinatra error -- JSON::ParserError - 784: unexpected tokenSinatra 错误 -- JSON::ParserError - 784: 意外令牌
【发布时间】:2017-11-22 02:40:30
【问题描述】:

我在 Sinatra (Mongodb) 中有一个 api。我正在尝试使用 POST 方法从用 Angularjs 编写的非常小的应用程序中写入一些数据。我要发送的数据是“aplication/x-www-form-urlencoded”。由于某种原因,我不断收到错误消息:

JSON::ParserError - 784: unexpected token at 'id=111&name=Bruce&city=LA&address=City1':

我可以看到url中编码了一些数据,但是Sinatra没有正确读取。

辛纳屈:

post '/companies' do
  company=JSON.parse(request.body.read)
  company = Company.new(company)
end

AngularJs 脚本:

var formApp = angular.module('formApp', []);
function formController($scope, $http) {
    $scope.formData = {};
    $scope.processForm = function() {
        $http({
            method  : 'POST',
            url     : 'http://localhost:4567/api/v1/companies',
            data    : $.param($scope.formData),  // pass in data as strings
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
            .success(function(data) {
                console.log(data);
                if (!data.success) {
                    // if not successful, bind errors to error variables
                    $scope.errorName = data.errors.name;
                    $scope.errorSuperhero = data.errors.superheroAlias;
                } else {
                    // if successful, bind success message to message
                    $scope.message = data.message;
                                $scope.errorName = '';
                    $scope.errorSuperhero = '';
                }
            });
    };
}

在HTML中完成表单后,我正在使用按钮调用函数formData:

body ng-app="formApp" ng-controller="formController">
<div class="container">
<div class="col-md-6 col-md-offset-3">

    <!-- PAGE TITLE -->
    <div class="page-header">
        <h1><span class="glyphicon glyphicon-tower"></span> Submitting Forms with Angular</h1>
    </div>

    <!-- SHOW ERROR/SUCCESS MESSAGES -->
    <div id="messages" class="well" ng-show="message">{{ message }}</div>

    <!-- FORM -->
    <form ng-submit="processForm()">


        <div id="superhero-group" class="form-group" ng-class="{ 'has-error' : errorSuperhero }">
            <label>ID</label>
            <input type="text" name="id" class="form-control" placeholder="Caped Crusader" ng-model="formData.id">
            <span class="help-block" ng-show="errorSuperhero">{{ errorSuperhero }}</span>
        </div>  


        <div id="name-group" class="form-group" ng-class="{ 'has-error' : errorName }">
            <label>Name</label>
            <input type="text" name="name" class="form-control" placeholder="Bruce Wayne" ng-model="formData.name">
                        <span class="help-block" ng-show="errorName">{{ errorName }}</span>
        </div>

        <!-- SUPERHERO NAME -->
        <div id="superhero-group" class="form-group" ng-class="{ 'has-error' : errorSuperhero }">
            <label>City</label>
            <input type="text" name="city" class="form-control" placeholder="Caped Crusader" ng-model="formData.city">
            <span class="help-block" ng-show="errorSuperhero">{{ errorSuperhero }}</span>
        </div>

                <div id="superhero-group" class="form-group" ng-class="{ 'has-error' : errorSuperhero }">
            <label>Address</label>
            <input type="text" name="address" class="form-control" placeholder="Caped Crusader" ng-model="formData.address">
            <span class="help-block" ng-show="errorSuperhero">{{ errorSuperhero }}</span>
        </div>

        <!-- SUBMIT BUTTON -->
        <button type="submit" class="btn btn-success btn-lg btn-block">
            <span class="glyphicon glyphicon-flash"></span> Submit!
        </button>
    </form>

    <!-- SHOW DATA FROM INPUTS AS THEY ARE BEING TYPED -->
    <pre>
        {{ formData }}
    </pre>

</div>

【问题讨论】:

标签: angularjs json ruby sinatra


【解决方案1】:

您正试图通过 JSON 解析器来解析 URL 编码数据。这行不通。

尝试将 response.body 解析为常规字符串而不是 JSON 字符串。

【讨论】:

    【解决方案2】:

    发生错误是因为 AngularJS 代码以 URL 编码形式发送数据。将数据作为JSON 字符串发布:

    var formApp = angular.module('formApp', []);
    function formController($scope, $http) {
        $scope.formData = {};
        $scope.processForm = function() {
            $http({
                method  : 'POST',
                url     : 'http://localhost:4567/api/v1/companies',
                ̶d̶a̶t̶a̶ ̶ ̶ ̶ ̶:̶ ̶$̶.̶p̶a̶r̶a̶m̶(̶$̶s̶c̶o̶p̶e̶.̶f̶o̶r̶m̶D̶a̶t̶a̶)̶,̶ ̶ ̶/̶/̶ ̶p̶a̶s̶s̶ ̶i̶n̶ ̶d̶a̶t̶a̶ ̶a̶s̶ ̶s̶t̶r̶i̶n̶g̶s̶
                data    : $scope.formData, //data automatically coded as JSON string
                ̶h̶e̶a̶d̶e̶r̶s̶ ̶:̶ ̶{̶ ̶'̶C̶o̶n̶t̶e̶n̶t̶-̶T̶y̶p̶e̶'̶:̶ ̶'̶a̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶/̶x̶-̶w̶w̶w̶-̶f̶o̶r̶m̶-̶u̶r̶l̶e̶n̶c̶o̶d̶e̶d̶'̶ ̶}̶            
            })
                ̶.̶s̶u̶c̶c̶e̶s̶s̶(̶f̶u̶n̶c̶t̶i̶o̶n̶(̶d̶a̶t̶a̶)̶ ̶{̶
                .then(function(response) {
                    ̲v̲a̲r̲ ̲d̲a̲t̲a̲ ̲=̲ ̲r̲e̲s̲p̲o̲n̲s̲e̲.̲d̲a̲t̲a̲;̲
                    console.log(data);
                    if (!data.success) {
                        // if not successful, bind errors to error variables
                        $scope.errorName = data.errors.name;
                        $scope.errorSuperhero = data.errors.superheroAlias;
                    } else {
                        // if successful, bind success message to message
                        $scope.message = data.message;
                                    $scope.errorName = '';
                        $scope.errorSuperhero = '';
                    }
                });
        };
    }
    

    $http 服务自动将 JavaScript 对象编码为 JSON 字符串。内容类型标头设置为application/json

    .success 方法已过时。请改用.then。如需更多信息,请参阅Why are angular $http success/error methods deprecated? Removed from v1.6?

    【讨论】:

    • 感谢您的帮助,我进行了更正,但我在控制台中收到 404 错误,而服务器上没有任何 xhanges。然后我也添加了标题,我得到了 t200 OK,但我的响应错误是:SyntaxError:JSON.parse:JSON 数据的第 1 行第 1 列的数据意外结束,然后我更改为 params 选项卡,我看到:{" id":"99","name":"李小龙","city":"上海","address":"MStreet"}: !我可以看到这个双冒号,我猜是错误的原因吧?
    • 它引导我找到正确的答案,所以我接受这个答案,谢谢你的帮助
    猜你喜欢
    • 2020-11-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多