2014-02-25 92 views
3

我正在處理一個ASP MVC應用程序,在一頁中我嘗試使用jQuery發出一個AJAX請求,並且我得到這個錯誤(在Firefox中):AJAX請求錯誤:「SyntaxError:JSON.parse:意外字符」

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="[email protected]" 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); 
      } 
     } 
    } 
+0

錯誤發生在哪裏?您應該能夠單擊錯誤消息直接跳到腳本中的位置(假設它在您的腳本中) – Phil

+0

我發現發生錯誤是因爲控制器中的操作從未被調用過,所以HTML頁面作爲響應返回,由於類型=「json」發生解析錯誤。我把這個動作移到了另一個控制器上,它工作正常。現在的問題是控制器的問題是什麼? –

+0

請你也可以用控制器的代碼更新這個問題嗎?至少這個動作和控制器body..rest調用你可以省略)。 –

回答

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

希望有所幫助。

+0

已嘗試此操作,但未作任何更改 –

+0

只需再次確認,如果將此操作移至其他控制器,則可以正常工作。對 ? –

+0

另一件我懷疑這裏的參數類型是GUID的應該是一個字符串。 (但正如你所說的那樣,正在另一個控制器工作,所以放棄了這種信念)。 –