gpt4 book ai didi

javascript - AJAX请求错误: "SyntaxError: JSON.parse: unexpected character"

转载 作者:行者123 更新时间:2023-12-01 01:46:36 24 4
gpt4 key购买 nike

我正在开发一个 ASP MVC 应用程序,在一个页面中我尝试使用 jQuery 发出 AJAX 请求,但收到此错误(在 Firefox 中):

SyntaxError: JSON.parse: unexpected character.

这是我的 JavaScript 函数:

function deleteGoal(id) {

var dataget = { "goalId": id };

$.ajax({
type: "GET",
url: '@Url.Action("DeleteGoal", "Goals")',
data: dataget,
dataType: "json",
success: function (json) {
if (json.isValid == false) {

Growl.error({
title: 'Error sending messages',
text: json.error
});
return false;
}
else {
alert("success");
}

},
error: function (xhr, status, error) {
alert(error);
},

});
}

通过以下方式调用:

<a class="btn btn-red" id="delete_@val.Id" onclick="javascript:deleteGoal('@val.Id')">@ViewBag.Translator.Translate("Delete")</a>

作为参数传递的 ID 是 GUID。

这是 Controller 的完整代码:

class GoalsController : BaseController
{
private const string ErrorViewPath = "../Shared/Error";

public ActionResult Goals(Guid? nodeId = null, string groupByCriteria = "Type", string sortCriteria = "LastModified")
{
try
{
ViewBag.Translator = SessionManager.Translator;
ViewBag.NodeId = nodeId;
if (!IsUserLogged())
{
return RedirectToAction("Login","Account");
}
if (!IsUserRegistered())
{
return RedirectToAction("Register", "Account", null);
}
if (SessionManager.UserStatus < (long)UserStatus.AmwayInitialized)
{
return RedirectToAction("Activation", "Account");
}

var res = Proxy.GetGoals(nodeId == null ? (Guid)SessionManager.NodeId : (Guid)nodeId);

if (res.HasErrors)
{
return ReportErrors(res, ErrorViewPath);
}
if (res.Value.Count == 0)
{
return View(new List<IGrouping<object,GoalContract>>());
}
ViewBag.groupBy = groupByCriteria;
ViewBag.sortBy = sortCriteria;

PropertyInfo sortPinfo = res.Value[0].GetType().GetProperty(sortCriteria);
PropertyInfo groupPinfo = res.Value[0].GetType().GetProperty(groupByCriteria);

res.Value.Sort(new Comparison<GoalContract>((x, y) => CompareGoalContract(x, y, sortPinfo)));
var groups = res.Value.GroupBy(g => g.GetType().GetProperty(groupByCriteria).GetValue(g));

return View(groups);
}
catch (Exception ex)
{
return ReportErrors(ex, ErrorViewPath);
}

}

private int CompareGoalContract(GoalContract t1, GoalContract t2, PropertyInfo property)
{
IComparable v1 = (IComparable)property.GetValue(t1);
IComparable v2 = (IComparable)property.GetValue(t2);
return v1.CompareTo(v2);
}

public ActionResult EditGoals(Guid? nodeId, string groupByCriteria = "Type", string sortCriteria = "LastModified")
{
try
{
ViewBag.Translator = SessionManager.Translator;
if (!IsUserLogged())
{
return RedirectToAction("Login", "Account");
}
if (!IsUserRegistered())
{
return RedirectToAction("Register", "Account", null);
}
if (SessionManager.UserStatus < (long)UserStatus.AmwayInitialized)
{
return RedirectToAction("Activation", "Account");
}
var res = Proxy.GetGoals(nodeId == null ? (Guid)SessionManager.NodeId : (Guid)nodeId);
if (res.HasErrors)
{
return ReportErrors(res, ErrorViewPath);
}
if (res.Value.Count == 0)
{
return View(new List<IGrouping<object, GoalContract>>());
}
ViewBag.groupBy = groupByCriteria;
ViewBag.sortBy = sortCriteria;

PropertyInfo sortPinfo = res.Value[0].GetType().GetProperty(sortCriteria);
PropertyInfo groupPinfo = res.Value[0].GetType().GetProperty(groupByCriteria);

res.Value.Sort(new Comparison<GoalContract>((x, y) => CompareGoalContract(x, y, sortPinfo)));
var groups = res.Value.GroupBy(g => g.GetType().GetProperty(groupByCriteria).GetValue(g));

return View(groups);
}
catch (Exception ex)
{
return ReportErrors(ex, ErrorViewPath);
}
}

[HttpPost]
[AllowAnonymous]
public ActionResult SaveGoal(GoalContract contract)
{
try
{
contract.LastModified = DateTime.UtcNow;
var result = Proxy.SaveGoal(contract);
return Json(new { error = result.PrintErrors(), isValid = !result.HasErrors});
}
catch (Exception ex)
{
return Json(new { error = ex.Message, isValid = false, isException = true });
}
}

[HttpPost]
public ActionResult DeleteGoal(Guid goalId)
{
try
{
var result = Proxy.DeleteGoal(goalId);
return Json(new { error = result.PrintErrors(), isValid = !result.HasErrors, isException = false }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { error = ex.Message, isValid = false, isException = true }, JsonRequestBehavior.AllowGet);
}
}
}

最佳答案

请更改

$.ajax({
type: "GET",
url: '@Url.Action("DeleteGoal", "Goals")',

$.ajax({
type: "POST",
url: '@Url.Action("DeleteGoal", "Goals")',

如果您不想发帖,请从操作中删除属性

[HttpPost]

让你的 Action 看起来像

public ActionResult DeleteGoal(Guid goalId)
{
try
{
var result = Proxy.DeleteGoal(goalId);
return Json(new { error = result.PrintErrors(), isValid = !result.HasErrors, isException = false }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { error = ex.Message, isValid = false, isException = true }, JsonRequestBehavior.AllowGet);
}
}

希望有帮助。

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

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