js如何调用webservice

lwp850210 2009-09-11 03:57:57
webservice 是删除数据库的一条记录 vs2008写的
网上找的方法 可是一直错误

<html>
<head>
<script language="javascript">

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"http://www.w3.org/2001/XMLSchema"http://schemas.xmlsoap.org/soap/envelope/">';
data = data + '<soap:Body>';
data = data + '<DeleteDraftRecord xmlns="http://tempuri.org/">';
data = data + '<recordID>'+value+'</recordID>';
data = data + '</DeleteDraftRecord>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL="http://localhost:81/ws/Service1.asmx";
xmlhttp.Open("POST",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/DeleteDraftRecord");
xmlhttp.Send(data);

var result = xmlhttp.status;
if(result==200) {
document.write(xmlhttp.responseText);
}
alert(result);
}
</Script>

</head>

<body>
<input type="button" value="del" onclick="javascript:RequestByPost(99)"/>
</body>
</html>


result一直是400
大家帮忙看看
...全文
927 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
玉麟龙 2011-09-24
  • 打赏
  • 举报
回复
17楼说的那么好,没分
玉麟龙 2011-09-24
  • 打赏
  • 举报
回复
分呢?还有天理吗?肉牛满面。。。。。。
晓风拂月 2010-08-17
  • 打赏
  • 举报
回复
var data;
var strMobilePhone="13761182978";
var strYDKey="cnsec20080512";
var strContent="Hello Word";
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 + '<WebCntSendSmsSP xmlns="http://www.cnsec.ocm/">';
data = data + '<strMobilePhone>'+strMobilePhone+'</strMobilePhone>';
data = data + '<strYDKey>'+strYDKey+'</strYDKey>';
data = data + '<strContent>'+strContent+'</strContent>';
data = data + '</WebCntSendSmsSP>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
var url="http://192.168.1.92/usercenter3/LogonWS.asmx";
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("post",url,false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("Content-Length",data.length);
xmlhttp.SetRequestHeader ("host","192.168.1.92");
xmlhttp.SetRequestHeader ("SOAPAction", "http://www.cnsec.com/WebCntSendSmsSP");
xmlhttp.send(data);
var x = xmlhttp.responseXML;
//alert(x);
//alert(x.childNodes[1].text);
var temp=x.childNodes[1].text;
if (temp==-10){
alert("验证失败"+temp);
}else if (temp==1){
alert("发送短信成功!");
}
lwp850210 2009-09-11
  • 打赏
  • 举报
回复
知道了 应该允许开放 HTTP-GET 与 HTTP-POST 协议
配置文件加上
<webServices>
<protocols>
<add name="HttpPost" />
<add name="HttpGet" />
</protocols>
</webServices>
zhxingway 2009-09-11
  • 打赏
  • 举报
回复
关注,只是知道有WebService这个东西.
chen_ya_ping 2009-09-11
  • 打赏
  • 举报
回复
还有jquery来调用webservice也是很方便。楼主这方面网上很多的资料
PSSonyXbox 2009-09-11
  • 打赏
  • 举报
回复
如果要是调用webservice的话应该有xml文档说呀,,
然后可以按说明来调用呀
程序漫步 2009-09-11
  • 打赏
  • 举报
回复
顶~~~
chen_ya_ping 2009-09-11
  • 打赏
  • 举报
回复
微软asp.net ajax客户端来调用webservice其实是非常的简单的。楼主可以试试
bzhyan 2009-09-11
  • 打赏
  • 举报
回复
UP
zyug 2009-09-11
  • 打赏
  • 举报
回复
你应该为readystatechange添加方法
而不是写在方法的结尾,这样永远是400

lwp850210 2009-09-11
  • 打赏
  • 举报
回复
大侠们呢
lwp850210 2009-09-11
  • 打赏
  • 举报
回复
up
lwp850210 2009-09-11
  • 打赏
  • 举报
回复
上面两位大哥 我就是按照那篇文章写的 但就是不对啊
wenblue7 2009-09-11
  • 打赏
  • 举报
回复
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>

ok,这给我们在静态页调用动态的内容提供了一种途径;
这里如果给getfemale()函数加上定时调用的话,就是一种无刷新更新页面的机制了。
缺点是webservice会有一定的延迟,即使是本地的webservice也会比静态页面慢很多,初次打开页面会感觉很不协调。


第二种方法使用了style.代码就简洁多了他使用了css.定义了div的行为.比起第一种方法,就易读多了:)

STYLE="behavior:url(webservice.htc)"

前提条件是:

If you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. The "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP).

附注:另一个总结帖子在:http://goody9807.cnblogs.com/archive/2005/08/17/216725.html


Calling WebServices using Javascript

If you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. The "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP).

To use the "WebService" behavior, you must attach it to an element using the STYLE attribute, as follows:


STYLE="behavior:url(webservice.htc)">

SK_Aqi 2009-09-11
  • 打赏
  • 举报
回复
顶起!
别样苍茫 2009-09-11
  • 打赏
  • 举报
回复
lwp850210 2009-09-11
  • 打赏
  • 举报
回复
up

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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