sharepoint使用jquery调用webservices或codebehind方法的多种形式
第一步不用说,当然是用vs创建webservices的project,然后强命名该dll。
然后更改.asmx的class引,类似:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ WebService Language="C#" Class="SchoolBundle.News.SiteGovern.WebService.SiteGovernWebPartWebService,SchoolBundle.News.SiteGovern.WebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5f573a266715e7ca" %>
在webservices的使用
后台:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[WebMethod]
public string GetScheduleContent(string _PlaceholderListUrl, string scheduledCategories)
{
string _re = string.Empty;
return _re;
}
前台:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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的
前台
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
$.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;
}
}
});