focus
focus

Reputation: 171

Post Json data to web methods

I have the following variable that post it normally to the following web method.

data: "{item:" + JSON.stringify(json) + ",step:" + JSON.stringify(john) + " }",

The web method:

[WebMethod(EnableSession = true)]
        public static string GetCart(string item, string step)
        {

            HttpContext.Current.Session["f"] = item;
            HttpContext.Current.Session["l"] = step;

            return item;


        }

When I try to add the following variable the 3rd variable(mytest) is not posted

data: "{item:" + JSON.stringify(json) + ",mytest:" + JSON.stringify(json) + ",step:" + JSON.stringify(john) + " }",

The web method

[WebMethod(EnableSession = true)]
        public static string GetCart(string item, string step, string mytest)
        {

            HttpContext.Current.Session["f"] = item;
            HttpContext.Current.Session["l"] = step;
            HttpContext.Current.Session["mytest"] = mytest;
            return item;


        }

Edit

And the post statment

$.ajax({

                type: 'POST',

                url: "mypage.aspx/GetCart",

                data: "{item:" + JSON.stringify(json) + ",mytest:" + JSON.stringify(json) + ",step:" + JSON.stringify(john) + " }",

                contentType: 'application/json; charset=utf-8',

                dataType: 'json'

Upvotes: 0

Views: 149

Answers (1)

Kirk B.
Kirk B.

Reputation: 456

You need double quotes:

...
data: "{\"item\":" + JSON.stringify(json) + ",\"mytest\":" + JSON.stringify(json) + ",\"step\":" + JSON.stringify(john) + " }"
...

Alternatively, you could stringify once:

...
data: JSON.stringify({item: json, mytest: json, step: john })
...

Upvotes: 1

Related Questions