经过几天研究,终于可以再单个js文件(纯JavaScript,不涉及AJax控件)调用WebServices了。现将调用方法及注意事项分享给大家
1、WebServices文件源码WebService.asmx
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string GetColumns(string strParameter)
{
return "{header: \"用户编号\",dataIndex: \"UserCode\",tooltip: \"用户编号\",sortable: true}, {header: \"用户名称\",dataIndex: \"UserName\",tooltip: \"用户名称\",sortable: true}, {header: \"用户类型\",dataIndex: \"UserType\",tooltip: \"用户类型\",sortable: true}, {header: \"性别\",dataIndex: \"Sex\",tooltip: \"性别\",sortable: true}, {header: \"职位名称\",dataIndex: \"PositionName\",tooltip: \"职位名称\",sortable: true}, {header: \"电话\",dataIndex: \"Telephone\",tooltip: \"电话\",sortable: true}, {header: \"邮箱\",dataIndex: \"Email\",tooltip: \"邮箱\",sortable: true}, {header: \"地址\",dataIndex: \"Address\",tooltip: \"地址\",sortable: true}, {header: \"部门名称\",dataIndex: \"DepartName\",tooltip: \"部门名称\",sortable: true}, {header: \"备注\",dataIndex: \"Remark\",tooltip: \"备注\",sortable: true}"; ;
}
}
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string GetColumns(string strParameter)
{
return "{header: \"用户编号\",dataIndex: \"UserCode\",tooltip: \"用户编号\",sortable: true}, {header: \"用户名称\",dataIndex: \"UserName\",tooltip: \"用户名称\",sortable: true}, {header: \"用户类型\",dataIndex: \"UserType\",tooltip: \"用户类型\",sortable: true}, {header: \"性别\",dataIndex: \"Sex\",tooltip: \"性别\",sortable: true}, {header: \"职位名称\",dataIndex: \"PositionName\",tooltip: \"职位名称\",sortable: true}, {header: \"电话\",dataIndex: \"Telephone\",tooltip: \"电话\",sortable: true}, {header: \"邮箱\",dataIndex: \"Email\",tooltip: \"邮箱\",sortable: true}, {header: \"地址\",dataIndex: \"Address\",tooltip: \"地址\",sortable: true}, {header: \"部门名称\",dataIndex: \"DepartName\",tooltip: \"部门名称\",sortable: true}, {header: \"备注\",dataIndex: \"Remark\",tooltip: \"备注\",sortable: true}"; ;
}
}
2、JavaScript文件源码 ClientLibrary.js
var objColoms;
CallWebServiceMethod(); //调用WebServices
function CallWebServiceMethod(data)
{
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var url="http://localhost:6808/JSWebServices/WebService.asmx/GetColumns?strParameter=zhangps";
xmlhttp.Open("Get",url, false);
xmlhttp.SetRequestHeader("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader("SOAPAction","http://tempuri.org/GetColumns");
xmlhttp.Send(data);
var result = xmlhttp.status;
if(result==200)
{
var responseText = xmlhttp.responseXML.text;
objColoms = "[sm," + responseText + "]";
}
xmlhttp = null;
}
//var extColumModel = new Ext.grid.ColumnModel(eval(objColoms));
CallWebServiceMethod(); //调用WebServices
function CallWebServiceMethod(data)
{
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var url="http://localhost:6808/JSWebServices/WebService.asmx/GetColumns?strParameter=zhangps";
xmlhttp.Open("Get",url, false);
xmlhttp.SetRequestHeader("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader("SOAPAction","http://tempuri.org/GetColumns");
xmlhttp.Send(data);
var result = xmlhttp.status;
if(result==200)
{
var responseText = xmlhttp.responseXML.text;
objColoms = "[sm," + responseText + "]";
}
xmlhttp = null;
}
//var extColumModel = new Ext.grid.ColumnModel(eval(objColoms));
3、Aspx页面或Html页面源码Default.aspx(近起到引用JavaScript文件作用)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JavaScript调用WebServices</title>
<script language="javascript" src="ClientLibrary.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
纯JavaScript调用WebServices例子
<!--注意:需要在WebConfig中加入以下配置:
<system.web>
<webServices>
<protocols>
<add name="HttpSoap" />
<add name="HttpPost" />
<add name="HttpGet" />
<add name="Documentation" />
</protocols>
</webServices>
</system.web>
//-->
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JavaScript调用WebServices</title>
<script language="javascript" src="ClientLibrary.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
纯JavaScript调用WebServices例子
<!--注意:需要在WebConfig中加入以下配置:
<system.web>
<webServices>
<protocols>
<add name="HttpSoap" />
<add name="HttpPost" />
<add name="HttpGet" />
<add name="Documentation" />
</protocols>
</webServices>
</system.web>
//-->
</div>
</form>
</body>
</html>
4、注意事项:Web.Config中必须做一下配置
<system.web>
<webServices>
<protocols>
<add name="HttpSoap" />
<add name="HttpPost" />
<add name="HttpGet" />
<add name="Documentation" />
</protocols>
</webServices>
</system.web>
<webServices>
<protocols>
<add name="HttpSoap" />
<add name="HttpPost" />
<add name="HttpGet" />
<add name="Documentation" />
</protocols>
</webServices>
</system.web>