VM1990:5未捕获的SyntaxError:意外的令牌:

时间:2016-02-20 13:58:56

标签: javascript jquery asp.net-mvc-5

我有一个MVC简单的应用程序。单击按钮时打开对话框。我的弹出内容是局部视图。

AddUserPartialView.cshtml:

@model  Demo.Models.AddUserViewModel

<script type="text/javascript">
   function function1() 
    {
       return  {'p1':'try1','p2':'try2'};
   }
   function function2() {
       return { 'p1': 'deneme1', 'p2': 'deneme2' };
   }
</script>
<div id="showErrorMessage"></div>

<form id="myForm">
    <div id="AddUserForm">
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name)
    </div>
</form>

HomeController中:

public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult AddUserPartialView()
        {
            return PartialView("AddUserPartialView", new AddUserViewModel());
        }

        [HttpPost]
        public JsonResult AddUserInfo(AddUserViewModel model)
        {
            bool isSuccess = false;
            if (ModelState.IsValid)
            {
                //isSuccess = Save data here return boolean
                isSuccess = true;
            }
            return Json(new { result = isSuccess, responseText = "Something wrong!" });
        }

和index.cshtml:

<div class="row">
    <div class="col-md-4">
        <button class="AddUser">Add User</button>
        <div id="AddUserForm"></div>
    </div>

</div>
<script>
    $(document).ready(function () {
    $('.AddUser').on('click', function () {
        $("#AddUserForm").dialog({
            autoOpen: true,
            position: { my: "center", at: "top+350", of: window },
            width: 1000,
            resizable: false,
            title: 'Add User Form',
            modal: true,
            open: function () {
                $(this).load('@Url.Action("AddUserPartialView", "Home")');
            },
            buttons: {
                "Add User": function () {
                    addUserInfo();
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
        return false;
    });
        function addUserInfo() {
            $.ajax({
                url: '@Url.Action("AddUserInfo", "Home")',
                type: 'POST',
                data: $("#myForm").serialize(),
                success: function (data) {
                    if (data.result) {
                        $(':input', '#myForm')
                            .not(':button, :submit, :reset, :hidden')
                            .val('')
                            .removeAttr('checked')
                            .removeAttr('selected');
                    } else {
                        $("#showErrorMessage").append(data.responseText);
                    }
                }
            });
        }
    });
</script>

如果在这样的function1()中(使用Enter字符):(在AddUserPartialView.cshtml中)

   return  
   {'p1':'try1','p2':'try2'};

我有错误的意外令牌。但是这样的function1(No Enter character):

return {'p1':'try1','p2':'try2'};

我没有错误。它工作正常。为什么?

1 个答案:

答案 0 :(得分:1)

您是自动分号插入的受害者。如果您有这样的代码:

return  
{'p1':'try1','p2':'try2'};

它被解释为:

return;
{'p1':'try1','p2':'try2'};

因此return语句不返回任何内容,下一行中的{}被解释为block statement,因此在结果中它是这样的:< / p>

return;
'p1':'try1','p2':'try2';

请参阅What are the rules for JavaScript's automatic semicolon insertion (ASI)?