簡體   English   中英

將Json數據發布到Web方法

[英]Post Json data to web methods

我有以下將其正常發布到以下Web方法的變量。

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

網絡方法:

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

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

            return item;


        }

當我嘗試添加以下變量時,第三個變量(mytest)未發布

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

網絡方法

[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;


        }

編輯

和后期陳述

$.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'

您需要雙引號:

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

或者,您可以一次字符串化:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM