前端代码

$(function () {
    $("td input").click(function () {
        var device = $(this).parent("div").siblings("div").children("marquee").text();
        

        $.ajax({

            type: "Post",                                           //post方式
            url: "小米商城.aspx/Getit",                             //要传值的页面/该页面下接收该数据的静态方法
            contentType: "application/json; charset=utf-8",         //注意大小写
            data: "{device:'"+ device+"'}",                         //注意形式 字符串拼接起来后是字典 {device:'小米10'}
            success: function (res) {                               //传值成功后执行的函数

                if (res.d == "success") {
                    alert("加入购物车成功:" + device);
                } else if (res.d == "guest") {
                    alert("您还未登录");
                } else if (res.d =="purchased"){
                    alert(device+"已在购物车中")
                }
                console.log("传入成功",res.d);
            },
            error: function (result) {                              //传值失败后执行的函数
                console.log("传入失败");
            }


        });
    });
})

后端C#代码


using System.Web.Services;


[WebMethod]                                      //千万不要省略
public static string Getit(string device)        //该函数中的参数要与传进来的data中的键对应 如传入:data:{device='小米10'} 则该函数的形参就为string device
{

    //想要进行的操作
}

前端传输组代码

//购买按钮
    $(function () {
        $(".buy span input").click(function () {

            var rowNum = $("table tr").length - 1;
            var data = new Array();                                            //定义数组
            for (var i = 1; i <= rowNum; i++) {
                var checkBox = "table tr:nth-child(" + (i + 1) + ") td:nth-child(1) input";
                var device = "table tr:nth-child(" + (i + 1) + ") td:nth-child(2) span";
                var inputNum = "table tr:nth-child(" + (i + 1) + ") td:nth-child(4) input[type='text']";



                if ($(checkBox).prop("checked")) {
                    if ($(inputNum).val() == "0") {
                        alert("您的商品:" + $(device).text() + "已勾选但数量为0,故未购买");
                    }
                    else {
                        data.push({ "device": $(device).text(), "num":        //填充数据 parseInt($(inputNum).val()) });

                    }
                }


            }

            //未勾选商品
            if (data.length == 0) {
                alert("您还未勾选商品!");
            }
            //已勾选,向后台上传
            else {
                $.ajax({
                    type: "post",
                    url: "buyCart.aspx/Buy",
                    contentType: "application/json; charset=utf-8",
                    data: "{ data:'" + JSON.stringify(data) + "'}",            //将数组转换成json字符串
                    success: function (res) {
                        if (res.d == "success") {
                            alert("购买成功!");

                        }
                        else if (res.d == "error") {
                            alert("您还未勾选商品!");
                        }
                        else {
                            alert(res.d);
                        }

                    },
                    error: function (res) {
                        alert("错误");
                        console.log(res.d);
                    }
                })

                
            }


        })
    })

c#后端代码

using System.Web.Services;

[WebMethod]
public static string Buy(string data)
{
    //将json字符串转换成列表
    JArray ja = (JArray)JsonConvert.DeserializeObject(data); 
    
    //遍历列表
    for (int i = 0; i < ja.Count; i++)
    {
                   
        string device = ja[i].ToString()
    
    }


    //由于我列表里面的元素是字典,具体想怎么操作自己想想办法
}

报Failed to load resource: the server responded with a status of 401 (Unauthorized)解决办法:在App_Start文件夹中的RouteConfig.cs文件的路由关闭即可,修改后代码如下:

public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            //settings.AutoRedirectMode = RedirectMode.Permanent;
            settings.AutoRedirectMode = RedirectMode.Off;//关闭路由
            routes.EnableFriendlyUrls(settings);
        }
    }

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐