gpt4 book ai didi

jQuery/JSON 错误 "SyntaxError: JSON.parse: unexpected character"

转载 作者:行者123 更新时间:2023-12-03 22:58:43 31 4
gpt4 key购买 nike

我正在开发 MVC4 并尝试使用 JQuery 和 JSON 将值表单 View 传递到 Controller 。该查询正在提取网格内复选框的值。代码如下:

<script type="text/javascript">
function DeleteCustomer() {
var temp = "";
var id = "";

if (confirm("Are you sure to delete records?")) {
$('#myGrid table tr').each(function () {
if ($(this).find("input[id*='assignChkBx']").length > 0) {
if ($(this).find("input[id*='assignChkBx']")[0].checked == true) {
temp = $(this).find("input[id*='assignChkBx']").val();
if (temp != "" || temp != null) {
id = id + " " + temp;
temp = "";
}
} // End of Loop
}
}); //End of each Loop
$.ajax({
url: "Customer/DeleteCustomeByID",
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: "{'CustomerID':'" + id + "'}",
success: function (data) {
//alert('Records deleted');
$('#lblMessage').html('Records Deleted');
},
error: function (xhr, textStatus, err) {
alert('Error: ' + err);
//$('#lblMessage').html(err);
}
});
}
}

我的html代码如下:

<input type="button" id="btnDelete" value="Delete" title="Delete" onclick="DeleteCustomer()" style="color: Gray" />


@{
WebGrid grid = new WebGrid(Model, rowsPerPage: 15, ajaxUpdateContainerId: "myGrid");
}
@grid.GetHtml(
fillEmptyRows: false,
alternatingRowStyle: "alternate-row",
headerStyle: "grid-header",
footerStyle: "grid-footer",
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] {
grid.Column("", format: @<text><input class="check-box" type="checkbox" id="assignChkBx" value="@item.CustomerID" /></text>),
grid.Column("CustomerID", "CustomerID", canSort: true),
grid.Column("CompanyName", "Company Name", canSort: true),
grid.Column("ContactName", "Contact Name", canSort: true),
grid.Column("Address", "Address", canSort: false),
grid.Column("City", "City", canSort: true),
grid.Column("",
header: "Actions",
format: @<text>
@Html.ActionLink("Edit", "Edit", new { id=item.CustomerID} )
@Html.ActionLink("Delete", "Delete", new { id=item.CustomerID} )
</text>
)
})

当我单击删除按钮时,上面提到的 jquery 会将所选值发送到 Controller 。 Controller 代码编写如下:

[HttpPost]
public ActionResult DeleteCustomeByID(string CustomerID)
{
Customer customer = new Customer();
try
{
if (ModelState.IsValid)
{
string[] values = CustomerID.Split(' ');

for (int i = 1; i <= values.Length - 1; i++)
{
if (values[i].ToString().Trim() != "" || values[i].ToString() != null)
{
customer = db.Customers.Find(values[i].ToString());
db.Customers.Remove(customer);
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
return View(customer); // Error in Model, if any
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
validationErrors.Entry.Entity.GetType().FullName,
validationError.PropertyName,
validationError.ErrorMessage);
}
}

throw new Exception(); // You can also choose to handle the exception here...
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

当我单击删除按钮时,值将转到 Controller 并删除记录。但问题是,删除记录后返回 Controller 时,我收到以下错误:“SyntaxError: JSON.parse: Unrecognized token” for FireFox, “json parse error unrecognized token” '<'”(对于 Safari)和“错误:对象错误。”我搜索各种网站并尝试各种解决方案。但没有任何作用。我正在使用 Northwind 数据库。

提前致谢。

帕萨

最佳答案

根据the docs ,以下属性:

dataType: "json"

... 告诉 jQuery 您期望从服务器返回的数据类型。那么你的操作就是返回 HTML。因此,当 jQuery 尝试解析它所期望的 JSON 时,它会遇到 HTML,并给出此错误。

更改操作以返回 JsonResult,或将 dataType 设置为 “html”

关于jQuery/JSON 错误 "SyntaxError: JSON.parse: unexpected character",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16967289/

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