html:<%@ 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>无标题页</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}
function AsynRequest()
{
createXMLHttpRequest();
xmlHttp.open("post","http://localhost:1675/WebSite2/Service.asmx/GetServerTime",true); xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
UpdateClock(xmlHttp.responsesText);
}
}
};
xmlHttp.send("");
}
function UpdateClock(aTime)
{
document.getElementById("clock").innerHTML=aTime;
}
</script>
</head>
<body onload="AsynRequest()">
<form id="form1" runat="server">
<div>
<span id="clock"></span>
</div>
</form>
</body>
</html>
webservice:using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService] [ScriptService]
public class Service : System.Web.Services.WebService
{
public Service ()
{ //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public string GetServerTime()
{
return DateTime.Now.ToString();
}
}
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}
function AsynRequest()
{
createXMLHttpRequest();
xmlHttp.open("post","http://localhost:1675/WebSite2/Service.asmx/GetServerTime",true); xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
UpdateClock(xmlHttp.responsesText);
}
}
};
xmlHttp.send("");
}
function UpdateClock(aTime)
{
document.getElementById("clock").innerHTML=aTime;
}
</script>
</head>
<body onload="AsynRequest()">
<form id="form1" runat="server">
<div>
<span id="clock"></span>
</div>
</form>
</body>
</html>
webservice:using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService] [ScriptService]
public class Service : System.Web.Services.WebService
{
public Service ()
{ //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public string GetServerTime()
{
return DateTime.Now.ToString();
}
}
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>在web.conifg中加入。
http://blog.csdn.net/lynnlin1122/archive/2009/02/05/3864713.aspx
http://localhost:1675/WebSite2/Service.asmx/GetServerTime
老大,我用了第一种get方式也 调不到啊?
改成false看看
<!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>无标题页</title>
<script type="text/javascript">
var xmlHttp; function createXMLHttpRequest() {
if (window.ActiveXObject)
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest)
xmlHttp = new XMLHttpRequest();
} function AsynRequest() {
createXMLHttpRequest(); xmlHttp.open("GET", "Service.asmx/GetServerTime", true); xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
UpdateClock(xmlHttp.responseXML.selectNodes("//string")[0].text);
}
} };
xmlHttp.send("");
} function UpdateClock(aTime) {
document.getElementById("clock").innerHTML = aTime;
}
</script>
</head>
<body onload="AsynRequest()">
<form id="form1" runat="server">
<div>
<span id="clock"></span>
</div>
</form>
</body>
</html>
GetServerTime 只是ws的一个方法。
header注意需加上Content-type:text/xml;用用java的soap工具soapui吧,很方便,只需载入wsdl路径(你这个应该是http://localhost:1675/WebSite2/Service.asmx?wsdl),就可自动生成发送的xml+soap串。还可以查看具体的http header和body.
你将生成的串+参数(如果必要)作为http body发送到http://localhost:1675/WebSite2/Service.asmx既可。
为什么我那边就是不行, webservice 是不是还要配置什么啊?
<system.web>
<webServices>
<protocols>
<add name= "HttpPost" />
<add name= "HttpGet" />
</protocols>
</webServices>
</system.web>
http://topic.csdn.net/u/20100420/13/7a7d589f-82af-49cb-ba9b-481499cd1e82.html
实质就是利用javascript POST http 头加http soap格式化串body.
上面的方法XMLHttpRequest简单,无需构造SoapHead等
你这样的请求,返回只是xml,并非webservice方法执行的结果