SyntaxError:JSON.parse:JSON数据的第1行第2列的意外字符

时间:2015-02-02 05:06:11

标签: javascript jquery json asp.net-mvc

控制器

[HttpGet]
public ActionResult VerifyUserEmail(string User_Email)
{
  try
  {
    using (EmptyMVCApplicationEntities objConnection = new EmptyMVCApplicationEntities())
    {
      ObjectParameter objIErrorCode = new ObjectParameter("ErrorCode", typeof(Int32));  
      ObjectParameter objBFlag = new ObjectParameter("bFlg", typeof(bool));
      objConnection.Check_User_Exists(User_Email, objBFlag, objIErrorCode);
      if (Convert.ToBoolean(objBFlag.Value) != true)
      {
        return Json(new { Success = "false", Message = "Email exists" }, JsonRequestBehavior.AllowGet);
      }
      else
      {
        return Json(new { Success = "True", Message = "Email not exists" }, JsonRequestBehavior.AllowGet);
      }               
    }
  }
  catch (Exception Ex) 
  {    
  }
}

脚本

$("#User_Email").blur(function () {
  if ($(this).val() != "") {
    $.ajax({
      url: "/User/VerifyUserEmail?User_Email=" + $("#User_Email").val(),
      success: function (result) {
        try {
          var jsonIssueObj = $.parseJSON(result).Data;    
        }  catch (e) { alert(e); }
        if (!jsonIssueObj.Success) {
          var errorMsg = jsonIssueObj.Message;
          $('#msg').html(errorMsg);
          $('#msg').show();
        }
        else {
          var errorMsg = null;
          $('#msg').html(errorMsg);
          $('#msg').hide();
        }
      }
    });
  }
  return false;
});

我收到以下错误:

  

SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符

此外,如果我想在对象中使用Success和Message,那么如何在控制器actionresult中将对象作为json传递。

1 个答案:

答案 0 :(得分:0)

您使用2个属性SuccessMessage返回json(您不需要parseJSON)。要访问它们

success: function (result) {
  var success = result.Success;
  var message = result.Message;
  ....