AJAX请求错误:"语法错误:JSON.parse:意外字符"

时间:2014-02-25 04:15:12

标签: javascript jquery ajax json asp.net-mvc-4

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

  

SyntaxError:JSON.parse:意外字符。

这是我的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。

这是控制器的完整代码:

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

1 个答案:

答案 0 :(得分:0)

请更改

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

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

如果您不想发布帖子,请执行操作删除属性

[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);
        }
    }

希望有所帮助。