Js/Ajax中发送HttpPost请求调用WebService

1) WebService中的方法

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

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [System.ComponentModel.ToolboxItem(false)]

    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。

    // [System.Web.Script.Services.ScriptService]

    public class WebServiceDemo : System.Web.Services.WebService

    {

 

        [WebMethod]

        public string HelloWorld(string name)

        {

            return "Hello World,"+name;

        }

}

2) WebForm页面中的js代码

<script src="Scripts/jquery-1.7.1.js"></script>

    <script>

        $(function () {

            $.ajax(

                {

                    type: "post",

                    url: "http://localhost:10974/WebServiceDemo.asmx/HelloWorld",

                    data: "name=king",

                    success: function (msg) {

                        var text = msg.childNodes[0].textContent;

                        alert(text);

                    }

                }

                );

        });

    </script>

3)执行结果

posted @ 2013-12-08 13:21  kinglong1984  阅读(1066)  评论(0编辑  收藏  举报