JQuery 调用 .net WebService

//web service 方法

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService] //这句一定要加
public class WebService_test : System.Web.Services.WebService {

    public WebService_test() {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string ImportStudent(Dictionary<string, string>[] students) {
        if (students.Length == 0) {
            return "没有任何数据!";
        }
        else {
            try {
                foreach (Dictionary<string, string> stu in students) {
                    //构造一个新的Student对象。
                    Student student = new Student();

                    //为新构造的Student对象属性赋值。
                    foreach (string key in stu.Keys) {
                        switch (key) {
                            case "name":
                                student.Name = stu[key];
                                break;
                            case "sex":
                                student.Sex = stu[key];
                                break;
                            case "age":
                                int age;
                                if (Int32.TryParse(stu[key], out age)) {
                                    student.Age = age;
                                }
                                else {
                                    student.Age = 0;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
                return "导入学生成功!";
            }
            catch {
                throw new Exception("导入学生失败!");
            }
        }
    }

    class Student {
        public Student() {
        }
        private string _name = "";
        public string Name {
            get { return _name; }
            set { _name = value; }
        }

        private string _sex = "";
        public string Sex {
            get { return _sex; }
            set { _sex = value; }
        }
        private int _age = 0;
        public int Age {
            get { return _age; }
            set { _age = value; }
        }
    }
}
 

//JQuery 调用 

    (function JQCallWS(){

        $.ajax({

            url: "http://localhost:8817/lqbz/WebService_test.asmx/ImportStudent",

            type: "post",

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

            dataType: "json",

            data: "{'students':[{'name':'KoBe','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]}",

            success: function(result) {

                alert("JQ" + result);

            },

            error: function(e) {

                alert(e.responseText);

            }

        });  

    })();

 RascallySnake  原文链接 

//js 调用

function ajaxFunction() {

    try {

        // Firefox, Opera 8.0+, Safari

        xmlHttp = new XMLHttpRequest();

    }

    catch(e) {

        // Internet Explorer

        try {

            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");

        }

        catch(e) {

            try {

                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

            }

            catch(e) {

                alert("您的浏览器不支持AJAX!");

xmlHttp = false;

            }

        }

    }

return xmlHttp;

}

(function(){

var xmlHttp = ajaxFunction();

xmlHttp.open("POST","http://localhost:8817/lqbz/WebService_test.asmx/ImportStudent",true);

xmlHttp.setRequestHeader("Content-Type", "application/json;charset=utf-8");

xmlHttp.onreadystatechange = function(){

if(xmlHttp.readyState == 4){

num = xmlHttp.readyState;

response = xmlHttp.responseText;

alert(response);

}

}

xmlHttp.send("{'students':[{'name':'KoBe','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]}");

})();

 需要注意的是,服务端参数名需要和客户端Json数组的key值相同


 

转载于:https://www.cnblogs.com/enzo/articles/1708284.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值