Jquery调用WebService


最近帮别人面试SharePoint Developer,问他们项目开发时他们都提到了在一个部署到SharePoint页面中用javascript调用SharePoint WebService, 之前一直用SharePoint Service/Client API开发, 用C#代码调用过WebService,但是没有用JS试过,今天试了下,发现点使用上的问题,不费话了,直接上图上代码.

 

1.先建一个ASP.NET的工程,下载一个jQuery类库添加到工程中,再在工程中添加一个WebService

 



 


 

2. 在WebService中添加以下代码

namespace WebApplication4
{
   /// <summary>
   /// Summary description for MyWebService
   /// </summary>
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
   // To allow this Web Service to be called from script, using ASP.NETAJAX, uncomment the following line.
   [System.Web.Script.Services.ScriptService]
   public class MyWebService : System.Web.Services.WebService
   {
 
        [WebMethod]
        public string HelloWorld(string msg)
        {
            return msg;
        }
 
   }
}

 

[System.Web.Script.Services.ScriptService]这一句默认是注释掉的,在Jquery中调用WebService时,如果返回xml格式没有问题,如果想返回json对象,这句注释必须打开

建完的WebService如下:


 

 

3. Jquery中调用WebService:

 

<html>
<head>
<scriptsrc="jquery-1.10.2.min.js"type="text/javascript"></script>
</head>
<body>
<scripttype="text/javascript">
   $(document).ready(function () {
        $.ajax({
            type: "POST",
            url:"/MyWebService.asmx/HelloWorld",
            data: "msg=123",
            //contentType:"application/x-www-form-urlencoded; charset=UTF-8",
            dataType: "xml",
            success: function (data,textStatus) {
                if (textStatus =="success") {
                    alert($(data).text());
                }
            },
            error: function (data, status,error) {
                alert("error");
            }
        });
   });
</script>
</body>
</html>


这种调用指定dataType是xml,它的意思是返回类型是xml类型,contentType不用指定(默认是application/x-www-form-urlencoded;charset=UTF-8)

 

下面看另一个返回格式-json

 

<html>
<head>
<script src="jquery-1.10.2.min.js"type="text/javascript"></script>
</head>
<body>
<scripttype="text/javascript">
   $(document).ready(function () {
        var pdata = { "msg":"hello" };
        $.ajax({
            type: "POST",
            url: "/MyWebService.asmx/HelloWorld",
            data: "{'msg':'123'}",
            contentType:"application/json; charset=UTF-8",
            dataType: "json",
            success: function (data,textStatus) {
                if (textStatus =="success") {
                    alert(data.d);
 
                }
            },
            error: function (data, status,error) {
                alert("error");
            }
        });
   });
</script>
</body>
</html>

dataType返回的是json对象,必须指定contentType为 application/json; charset=UTF-8


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值