简体   繁体   English

将Json数据发布到Web方法

[英]Post Json data to web methods

I have the following variable that post it normally to the following web method. 我有以下将其正常发布到以下Web方法的变量。

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 当我尝试添加以下变量时,第三个变量(mytest)未发布

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'

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 })
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM