Hi all,
i am trying to sent json object from client side jquery to server side using asp.net c#
i have done all the respected codes given below.
But i unable to get the output instead of that i got the below message,
please let me know what is the problem occur in my code.
---------------------------
Message from webpage
---------------------------
Success :No Data
---------------------------
OK
---------------------------
please find my below code:
My json object Code:
<script type="text/javascript">
$(function ()
{
$('#jstree2').jstree({'plugins':["wholerow","checkbox"], 'core' :
{
type: "POST",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
'data' : [
{ "id" : "ajson1", "parent" : "#", "text" : "Simple"},
{ "id" : "ajson2", "parent" : "#", "text" : "Root" },
]
}
}
);});
</script>
My json object sent to server side code:
<script type="text/javascript">
$(function () {
$("#Button1").on('click', function (e) {
var obj=JSON.parse(JSON.stringify(jQuery('#jstree2').serializeArray()))
$.ajax({
type: "POST",
url: "Handler.ashx",
data: obj,
contentType: "application/json; charset=UTF-8",
beforSend: function () {
$(this).attr("disabled", "true");
$(this).after(waitObj);
},
success: function (msg) {
console.log(msg);
alert("Success :" + msg);
},
error: function (msg) {
alert(msg);
}
});
});
});
</script>
My html code:
<div id="jstree2" style="margin-top:2em;">
</div>
<input id="Button1" type="button" value="button"/>
My server side code:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Web.Script.Serialization;
using System.IO;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
try
{
string strJson = new StreamReader(context.Request.InputStream).ReadToEnd();
userInfo objUsr = Deserialize<userInfo>(strJson);
if (objUsr != null)
{
string idname = objUsr.id;
string parentname = objUsr.parent;
string textname = objUsr.text;
context.Response.Write(string.Format("id :{0} , parent={1}, text={2}", idname, parentname, textname));
}
else
{
context.Response.Write("No Data");
}
}
catch (Exception ex)
{
context.Response.Write("Error :" + ex.Message);
}
}
public bool IsReusable {
get {
return false;
}
}
public class userInfo
{
public string id { get; set; }
public string parent { get; set; }
public string text { get; set; }
}
public T Deserialize<T>(string context)
{
string obj = context;
var jsonData = (T)new JavaScriptSerializer().Deserialize<T>(obj);
return jsonData;
}
}
thanks in advance..
What I have tried:
json object sent from client side jquery to server side