Geeks With Blogs
Łukasz Kuryło's blog

I have lost recently some time to send a json parameter to my web service using jquery.

Suppose that we have the data in an javascript array:

var attributes=[];
$('#PreviewGroup1_DecisionAttributesList input:checkbox').each(function(index,elem){ 	
	attributes.push({attribute: elem.value, checked: elem.checked});

To send that array to web service we simply need to do

	url: '../Services/PreviewFormWebService.asmx/UpdateDecisionAttributes',
	dataType: "json",
	data: "attrs="+ JSON.stringify(attributes),
	contentType: "application/json; charset=utf-8"

and in the web method:

[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] 
public void UpdateDecisionAttributes( )
System.Web.HttpContext context = System.Web.HttpContext.Current; 
var attributes = context.Request["attrs"];

 The  JSON.stringify() can be found here:


The string from result can be of course deserialized in easy way, e.g.:<

var attributes = context.Request["attrs"];
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DecisionAttributeJavaScriptConverter() });
var deserializedData = serializer.Deserialize

and the converter:

    public class DecisionAttribute
        public string Name { set; get; }
        public bool IsDecisionAttr { set; get; }

public class DecisionAttributeJavaScriptConverter : JavaScriptConverter
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
            return new DecisionAttribute()
                Name = Convert.ToString(dictionary["attribute"]),
                IsDecisionAttr = Convert.ToBoolean(dictionary["checked"])

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
            throw new NotImplementedException();

        public override IEnumerable<Type> SupportedTypes
                return new ReadOnlyCollection<Type>(new Type[] { typeof(DecisionAttribute) });

Posted on Monday, May 2, 2011 7:38 PM other | Back to top

Comments on this post: Sending json to xml web service as a parameter.

Comments are closed.
Comments have been closed on this topic.
Copyright © Łukasz Kuryło | Powered by: