Click here to Skip to main content
16,017,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
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.


C#
---------------------------
Message from webpage
---------------------------
Success :No Data
---------------------------
OK   
---------------------------


JavaScript
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>



JavaScript
My json object sent to server side code:

<script type="text/javascript">
  $(function () {
  
    $("#Button1").on('click', function (e) {

 //  var obj=JSON.stringify(jQuery('#jstree2').serializeArray());
   //var obj=JSON.stringify(jQuery('#jstree2').serializeArray()); // store json string
   var obj=JSON.parse(JSON.stringify(jQuery('#jstree2').serializeArray())) // store json object
    $.ajax({
        type: "POST",
        //url: "Default3.aspx/login",
        url: "Handler.ashx",
        data: obj,
        contentType: "application/json; charset=UTF-8",
       // dataType: "json",

        beforSend: function () {
            $(this).attr("disabled", "true");
            $(this).after(waitObj);
        },

        success: function (msg) {
            // Replace the div's content with the page method's return.

   console.log(msg);
            alert("Success :" + msg);
        },
        error: function (msg) {
         alert(msg);
        }
       
    });
});
});
	
</script>


HTML
My html code:

   <div id="jstree2" style="margin-top:2em;">

   </div>
<input id="Button1" type="button" value="button"/>



C#
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();

            //deserialize the object
            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;
        }
    }
    // we create a userinfo class to hold the JSON value
    public class userInfo
    {
        public string id { get; set; }
        public string parent { get; set; }
        public string text { get; set; }
       
    }


    // Converts the specified JSON string to an object of type T
    public T Deserialize<T>(string context)
    {
        string obj = context;

        //cast to specified objectType
        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
Posted
Updated 4-Mar-16 6:01am
Comments

Post the Json back to service side as string.

Then use Json.NET to convert to a .NET object.

Have a look at : Json.NET - Newtonsoft[^]
 
Share this answer
 
Hi stellus.

In use a method done example:
JavaScript
$.ajax({ url: "test.html", context: document.body })
 .done(function(data) {
    console.log(data.Name);
 });


I hope, it has be useful
 
Share this answer
 

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