初学webservice 帮忙找个错误

luofuxian 2009-09-15 11:06:28
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;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


/// <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 HelloWorld() {

return "Hello World";
}

}


Default.aspx:

<%@ 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">
function getHello()
{
WebService.HelloWorld(callComplete);
}
function callComplete(result)
{
alert(result);
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<button onclick="getHello()">
hello world</button>
<asp:ScriptManager runat="server" ID="scriptManager">
<services>
<asp:servicereference path="WebService.asmx" />
</services>
</asp:ScriptManager>
</form>
</body>
</html>

问题点击button的时候,总说WebService 未定义?
...全文
175 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
bfmdywks 2009-09-17
  • 打赏
  • 举报
回复
1。
在WebService定义处有如下注释
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
所以应该去掉注释,
[System.Web.Script.Services.ScriptService]

2. 如果还不行,试下:
<asp:ScriptManager runat="server" ID="scriptManager">
<services>
<asp:servicereference path="WebService.asmx" InlineScript="true" />
</services>
</asp:ScriptManager>


zhongjiekangping 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 luofuxian 的回复:]
C# code
[System.Web.Services.WebMethod]publicstaticstring IsUserNameExist(string userName)
{
System.Threading.Thread.Sleep(100);if (userName.Trim()!="huangbo")
{return"ok";
}else
{return"对不起,你输入的用户名"+ userName+"已被他人占用,请选择其他名字后再试";
}
}
这种写在codebehind的方式,基本会了,现在是怎么调用*.asmx里面的不会或者说:不能这么调用?
[/Quote]

对于webservice 的调用, 首先要生成 代理类
两种方法:
1 通过wsdl.exe 直接生成.cs 文件 然后添加到客户端程序,
2 在客户端程序 add web reference
生成 代理类之后 则可以通过它 直接调用 webservice 方法

Service serverObject = new Service(); // 代理类对象
txtResult.Text = serverObject.testWebService(); // 方法调用
SK_Aqi 2009-09-15
  • 打赏
  • 举报
回复
测试下吧
SK_Aqi 2009-09-15
  • 打赏
  • 举报
回复
我这里测试下和你一样的问题,只是没有添加httpHandlers
    <httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>

</system.web>里
luofuxian 2009-09-15
  • 打赏
  • 举报
回复
这个问题很难吗,明天加分,哪位老大 路过解决下,先谢谢了。
luofuxian 2009-09-15
  • 打赏
  • 举报
回复
会不会是我哪个步骤少了...
luofuxian 2009-09-15
  • 打赏
  • 举报
回复
我是参照文章:http://www.cnblogs.com/interboy/archive/2006/11/26/573045.html
按照上面讲的做的,如果是用ajax来做,就不用问了;
luofuxian 2009-09-15
  • 打赏
  • 举报
回复
@SK_Aqi
这样写就好像纯ajax了,有其他方式吗?
SK_Aqi 2009-09-15
  • 打赏
  • 举报
回复
js调用post,get两种调用,当然也是楼主说的codebehind,还有AJAX控件调用但是这个没有拉控件不行的吧?当然还有动态调用.哈哈

引用一部分:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string SayHelloTo(string Name) {
return "Hello "+Name;
}

}
还是俗了点。:)

2. js调用webservice+xmlhttp的实现部分。

<html>
<title>
Call webservice with javascript and xmlhttp.
</title>
<body>
<script language="javascript">

//Test function with get method.
function RequestByGet(data){

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";
xmlhttp.Open("GET",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
var result = xmlhttp.status;
//OK
if(result==200) {
document.write(xmlhttp.responseText);
}
xmlhttp = null;
}

//Test function with post method
function RequestByPost(value)
{
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<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/">';
data = data + '<soap:Body>';
data = data + '<SayHelloTo xmlns="http://tempuri.org/">';
data = data + '<Name>'+value+'</Name>';
data = data + '</SayHelloTo>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL="http://localhost:1323/WebSite6/Service.asmx";
xmlhttp.Open("POST",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
document.write( xmlhttp.responseText);

}

</Script>

<input type="button" value="CallWebserviceByGet" onClick="RequestByGet(null)">
<input type="button" value="CallWebserviceByPost" onClick="RequestByPost('Zach')">

</body>
</html>
对于使用post方法需要发送的那堆东东可以在webservice的测试页面中找到,自己拼凑加上对应的参数就可以。

通过Style.BEHAVIOR来实现的方法(比较简单)

原文地址:http://www.zahui.com/html/4/37953.htm

<script language="javascript">
function getfemale()
{
//第一个参数是webservice的url,后面是名称
female.useService("news.asmx?WSDL","news");
//设置一个回调函数,service返回结果的时候回调;第一个参数是回调函数的名称,后面的是webservice的参数
intCallID=female.news.callService(female_result,"getphoto","female"); //这里有两个参数.....
}

function female_result(result)//回调函数
{
if(result.error)
{
female.innerHTML=result.errorDetail.string;
}
else
{
female.innerHTML=result.value; //将webservice返回的结果写如div中
}
}
</script>
页面显示部分: <div id="female" style="BEHAVIOR:url(WebService.htc)"></div>

luofuxian 2009-09-15
  • 打赏
  • 举报
回复

[System.Web.Services.WebMethod]
public static string IsUserNameExist(string userName)
{
System.Threading.Thread.Sleep(100);
if (userName.Trim() != "huangbo")
{
return "ok";
}
else
{
return "对不起,你输入的用户名" + userName + "已被他人占用,请选择其他名字后再试";
}
}

这种写在codebehind的方式,基本会了,现在是怎么调用*.asmx里面的不会或者说:不能这么调用?
SK_Aqi 2009-09-15
  • 打赏
  • 举报
回复
不可以这么写,cs文件中就可以了.
luofuxian 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhongjiekangping 的回复:]
WebService.HelloWorld(callComplete);
你这调用有问题吧  又不是静态的
[/Quote]
那怎么调?
zhongjiekangping 2009-09-15
  • 打赏
  • 举报
回复
WebService.HelloWorld(callComplete);
你这调用有问题吧 又不是静态的

12,166

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧