sharepoint使用jquery调用webservices或codebehind方法的多种形式

第一步不用说,当然是用vs创建webservices的project,然后强命名该dll。

 

然后更改.asmx的class引,类似: 

 

WebService Class
<%@ WebService Language="C#" Class="SchoolBundle.News.SiteGovern.WebService.SiteGovernWebPartWebService,SchoolBundle.News.SiteGovern.WebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5f573a266715e7ca" %>

 

在webservices的使用
 后台:

webservices string
[WebMethod]
public string GetScheduleContent(string _PlaceholderListUrl, string scheduledCategories)
{
string _re = string.Empty;

return _re;
}


 前台:

 

js
function GetScheduleContent() {
var soapEnv = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
' <soap:Body>' +
' <GetScheduleContent xmlns="http://tempuri.org/">' +
' <_PlaceholderListUrl>' + SiteGovernPlaceholderListUrl + '</_PlaceholderListUrl>' +
' <scheduledCategories><![CDATA[' + scheduledCategories + ']]></scheduledCategories>' +
' </GetScheduleContent>' +
' </soap:Body>' +
' </soap:Envelope>';
$.ajax({
type:
"POST",
url:
"/_vti_bin/SiteGovernWebPartWebService.asmx",
beforeSend:
function(xhr) {
xhr.setRequestHeader(
"SOAPAction", "http://tempuri.org/GetScheduleContent");
},
contentType:
"text/xml; charset=\"utf-8\"",
dataType:
"xml",
data: soapEnv,
complete:
function(xData, status) {
$(
"#Div_scheduledCategories").html($(xData.responseXML).find("GetScheduleContentResult").text());
}
});
}

 

soapEnv的格式,在浏览器中查看.asmx的时候可以看到
soapEnv中的
scheduledCategories传入的是xml格式的,所以需要加上<![CDATA[]]>


调用页面方法
后台
[WebMethod]
public static string CheckPublishingPageNameExist(string _PageName)
{
return “”;
}

方法必须声明为static的

前台
代码
$.ajax({
type:
"POST",
url:
"PublishPage.aspx/CheckPublishingPageNameExist",
data:
"{'_PageName':'" + pagenamevalue.value + "'}",
contentType:
"application/json; charset=utf-8",
dataType:
"json",
async:
false,
success:
function(msg) {
if (parseInt(msg.d) == 0) {
re
= true;
}
}
});
posted @ 2010-02-22 15:10  geek007  阅读(491)  评论(0编辑  收藏  举报