【问题标题】:Jquery POST reporting undefined, JSON parse resulting in Syntax errorJquery POST 报告未定义,JSON 解析导致语法错误
【发布时间】:2014-04-29 10:10:21
【问题描述】:

这个想法相当简单,我们有一个创建复选框和一些静态复选框的灰尘模板。

  <div class="span10">
                <table class="table table-condensed">
                <caption class="pull-left">Favorite Cuisines:</caption>
                   <input type="hidden" id="myArray" />
                <tbody>
                    <tr>
                        {#data.cuisines}
                        <td>
                            <label class="checkbox inline">
         <input type="checkbox" name="cuisineFavorites" value="{_id}">{cuisineName}
                            </label>
                        </td>
                        {/data.cuisines}

                    </tr>
                    <tr>
                        <td>
                            <label class="checkbox inline">
                                <input type="checkbox" name="cuisineFavorites" value="name"> blank
                            </label>
                        </td>
                        <td>
                            <label class="checkbox inline">
                                <input type="checkbox" name="cuisineFavorites" value="american"> American
                            </label>
                        </td>
                        <td>
</div> 

sn-p 但是有一个过度的想法,它使用隐藏的输入 myArray 来使用 jquery 存储选定的复选框。在实际情况下,有很多复选框。

{@eq key="userProfile" value=prtName}
<script type="text/javascript">
    var selectedCuisines = [];
    function addCuisine(cuisine) {
    if ($.inArray(cuisine, selectedFavorites) < 0) {
        selectedCuisines.push(cuisine);
        selectedCuisines.sort();
    }
    $("#myArray").val("[" + selectedCuisines.join() + "]");
    }
    function removeCuisine(cuisine) {
    var pos = $.inArray(cuisine, selectedCuisines);
    if (pos > 0) {
        selectedCuisines.splice(pos, 1);
        selectedCuisines.sort();
    }
    $("#myArray").val("[" + selectedCuisines.join() + "]");
    }

    $('input[name^=cuisine]').on("click", function(e) {
        if (this.checked === true) {
        addCuisine(this.value);
        } else {
        removeCuisine(this.value);
        }
    });
    </script>
{/eq}

这应该将选定的复选框存储在 myarray 中以发送邮件。所以在 nodejs 控制器中我们输出了一些东西,或者至少尝试并尝试解析 myarray 中的值。大部分问题来自于未定义的传递

exports.createProfile = function (req, res) {
console.log(JSON.stringify(req.body.myArray));
var profile = new Profile(req.body);
console.log("print something"); 
profile.cuisineFavorites = JSON.parse(req.body.myArray); 
profile._userId = req.user._id;
console.log("Saving profile:" + profile);
console.log("favorites" + JSON.stringify( profile.cuisineFavorites));
profile.save( function(err) {
if (err) {
    return res.render('users', {
    title: 'Your Profile',
    prtName: 'userProfile',
    error: utils.errors(err.errors),
    user: req.user
    });
}

return res.render('users', {
  title: 'Organization Profile',
  prtName: 'orgprofile',
  error: '',
  user: req.user
});
});

}

请帮忙,不知道为什么它未定义,即使 JSON.parse 仍然说未定义也发布了示例输出,但它也工作了几次

SyntaxError: Unexpected token u at Object.parse (native) at exports.createProfile

在这里工作

  Model: {"title":"User Profile","prtName":"userProfile","user":{"_id":"532b781934
  fa1a7411000003","__v":0,"authToken":"","salt":"1387859137798","hashed_password":
  "e704f5c86a696a828fbb976446ee1243690b8917","provider":"local","username":"dw","g
  ivenName":"dw","familyName":"dw"},"data":{"cuisines":[{"_id":"532a4d281053cd042c
  000002","__v":0,"cuisineName":"American"},{"_id":"532a5c591053cd042c000003","__v
  ":0,"cuisineName":"Comfort Food"},{"_id":"532a5c5e1053cd042c000004","__v":0,"cui
  sineName":"Indian"},{"_id":"532a5c681053cd042c000005","__v":0,"cuisineName":"Med
  iterranean"},{"_id":"532a5c701053cd042c000006","__v":0,"cuisineName":"Portuguese
 "},{"_id":"532a5c791053cd042c000007","__v":0,"cuisineName":"Argentinian"}]}}

GET /users/profile/create 200 459ms

未定义 打印一些东西 保存配置文件:{ _userId: 532b781934fa1a7411000003, _id: 532b781f34fa1a7411000004, 美食最爱: [ 532a4d281053cd042c000002, 532a5c591053cd042c000003, 532a5c5e1053cd042c000004], 特殊饮食:[], 食物过敏: '', 办公室电话: '', 用户标题:'' } 收藏夹["532a4d281053cd042c000002","532a5c591053cd042c000003","532a5c5e1053cd0 42c000004"]

但通常只打印

undefined 打印一些东西,然后上面的错误就发生了。我最近更新了我的 klei-dust 安装,但这不应该影响 JSON

【问题讨论】:

    标签: jquery json node.js dust.js


    【解决方案1】:

    不要费心使用这种类型的方法来发送数据数组。我记得帖子是如何工作的。在这种情况下,Name = foodFavorites 对应于每个复选框。发送时选择的任何复选框都将放入名为美食收藏夹的数组中。 在这种情况下,当我们创建一个新的配置文件并将 req.body 设置为它时,配置文件会将 foodFavorites 设置为上一页上选择的任何一个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 2020-10-09
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      相关资源
      最近更新 更多