gpt4 book ai didi

javascript - VM1990 :5 Uncaught SyntaxError: Unexpected token :

转载 作者:行者123 更新时间:2023-12-03 07:38:12 25 4
gpt4 key购买 nike

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

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>

家庭 Controller :

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'};

我遇到意外 token 错误。但 function1 像这样(无输入字符):

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

我没有错误。效果很好。为什么 ?

最佳答案

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

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

它被解释为:

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

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

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

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

关于javascript - VM1990 :5 Uncaught SyntaxError: Unexpected token :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com