Click here to Skip to main content
15,887,428 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On clicking on Save button in view i have following code written.But It doesnt redirect to the url.If i dont mention The Url then also it automatically redirect to the corresponding controller HttpPost method..
JavaScript
$(document).ready(function () { alert("yy");
   $("#save").click(function () {
       var json = "{'USER_NAME':'" + escape(USER_NAME.val()) + "','MOBILE_NO':'" + escape(MOBILE_NO.val()) + "'}";
       alert(json);
       alert("RRR");
      // var ajaxPage = "~/Book/Employee/{0}";
       alert(ajaxPage);
        
       $.ajax(
       {
           type: "POST",
           url: ajaxPage,
           data: json,
           contentType: "application/json;charset=utf-8",
           dataType: "json",
           async: false,
           success: function (data) {
               if (data.Message == "Data Deleted Succeffully") {
                   showMessage(data.Message);
                   $('#table').dataTable().fnAddData();
                   filltable();

               }
               else {
                   showMessage(data.Message);
               }
           },
           error: function (msg) { alert("failed: " + msg); }
       });
   })
});
</script>

Plz help how to use ajax on view???
Posted
v3
Comments
Member 10435696 10-Feb-14 0:41am    
What changes were done..I am not able to find
Ahmed Bensaid 10-Feb-14 5:44am    
Do you have any error ?
Member 10435696 10-Feb-14 6:18am    
yes..i am getting SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data Error On controller
Member 10435696 10-Feb-14 6:20am    
[HttpPost]
public JsonResult Index(User model)
{
if (ModelState.IsValid)
{
JArray JsonArray = new JArray();
JObject jo = new JObject();
User puser=new User();
puser.USER_NAME = model.USER_NAME.Trim();
puser.EMAIL_ID = model.EMAIL_ID.Trim();
puser.MOBILE_NO = model.MOBILE_NO.Trim();
List<user> tUser = new List<models.user>();
tUser.Add(puser);
//GetPageBaseObject.Add(puser);

JsonArray = (JArray)JToken.FromObject(tUser);//.Replace(new Regex("\\n","g"),"");//.Replace(new Regex("\\n", "g"),"");

Response.Write(JsonArray);
jo["Message"] = "Data Saved Succeffully";
jo["data"] = JsonArray;

Response.Write(jo.ToString());
return Json(jo["data"]);
}
else
{
return Json(false);
}
}
}

here when i print Response.Write(JsonArray);
it gives this error and doesnt go to ajax success function on view

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900