Reputation: 135
Controller
[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)
{
}
}
Script
$("#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;
});
I am getting below error:
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
Also If I want Success and Message in an object, how to pass an object as json in controller actionresult.
Upvotes: 0
Views: 6764
Reputation:
You returning json with 2 properties, Success
and Message
(you do not need parseJSON
). To access them
success: function (result) {
var success = result.Success;
var message = result.Message;
....
Upvotes: 1