ajax访问webservice

hpp923 2009-06-08 09:38:42
js代码:
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp = new XMLHttpRequest();
}
function startRequest()
{
createXMLHttpRequest();
xmlHttp.open("GET","http://localhost:2366/WebSite28/WebService.asmx/Login",true);
xmlHttp.setRequestHeader('Content-Type','text/xml');
xmlHttp.setRequestHeader('charset','utf-8');
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
document.getElementById("aaa").innerText = xmlHttp.responseText;
}
}
xmlHttp.send("userName="+document.getElementById("userName").value+"&password="+document.getElementById("password").value);
}
</script>


WevService代码:

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 HelloWorld() {
return "Hello World";
}

[WebMethod(EnableSession=true)]
public int Login(string userName,string password)
{
if (userName == "admin" && password == "admin")
{
return 1;
}
else
{
return 0;
}
}
}



<input id="Button1" type="button" value="登录" onclick="startRequest()" />


点击登陆按钮没反应,请教高手解决
...全文
521 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wj7391438x 2011-05-11
  • 打赏
  • 举报
回复
爱仕达
alisafan123 2009-06-09
  • 打赏
  • 举报
回复
alert 一步一步测试
alisafan123 2009-06-09
  • 打赏
  • 举报
回复
function SetSession(name,value)
{
var x = new ActiveXObject("Microsoft.XMLDOM");
var xh = new ActiveXObject("Microsoft.XMLHTTP");
var url="../DefaultService.asmx/SetSessions" ;
xh.open("post",url,false);
xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") ;
xh.send("Name=" +name+ "&Value=" +value);
// x = xh.responseXML ;
// CodeIsValid = x.selectSingleNode("//SetSession/CodeIsValid") ;
// if ((CodeIsValid!=null) && (CodeIsValid.text=="true"))
// {
// alert("测试成功");
// }
// else
// {
// alert("测试失败");
// }
return false;
}

//用户检查商户编号是否有重复
function CustomerData(objstrcd,objtp,objtxt,oprtp)
{
if (objtxt.value=="")
{
objtxt.value="0";
}

var objs = objtp.getElementsByTagName("INPUT");
var value;
for(i=0 ; i<objs.length ; i++)
{
if(objs[i].checked==true)
{
value=objs[i].value;
break;
}
}
if (objstrcd.value=="")
{
alert("商户ID不可为空!");
return false;
}

var x = new ActiveXObject("Microsoft.XMLDOM");
var xh = new ActiveXObject("Microsoft.XMLHTTP");
var url="../DefaultService.asmx/CustomerDataRow" ;
xh.open("post",url,false);
xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") ;
xh.send("sStrcd=" +objstrcd.value+"&sTp="+value+"&sTxt="+objtxt.value+"&sOprtp="+oprtp);
x = xh.responseXML ;

CodeIsValid = x.selectSingleNode("//CustomerData/CodeIsValid") ;
RetMesg= x.selectSingleNode("//CustomerData/RetMesg") ;

if ((CodeIsValid!=null) && (CodeIsValid.text=="true"))
{
if(confirm("是否要进行保存")==true)
return true;
else
return false;
}
else
{
alert(RetMesg.text);
return false;
}
}


------------------------------------------------Web Service---------------------------

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Net;
using System.Net.Mail;
using Common;

/// <summary>
/// Summary description for DefaultService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DefaultService : System.Web.Services.WebService
{

public DefaultService()
{

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

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod(EnableSession = true)]
public void SetSessions(string Name, string Value)
{
Session[Name] = Value;
}

[WebMethod(Description = "检查商户ID的唯一性")]
public CustomerData CustomerDataRow(string sStrcd, string sTp, string sTxt, string sOprtp)
{
Common.CustomerData customerDataRow = new Common.CustomerData();

if (sStrcd == null && sStrcd == "")
{
customerDataRow.RetMesg = "该商户ID为空,请输入商户ID";
customerDataRow.CodeIsValid = false;
return customerDataRow;
}

try
{
if (sOprtp == "1")//增加时检查唯一性
{
DataAccess.CustomerInfo ap = new DataAccess.CustomerInfo();
if (ap.GetInfo(sStrcd) == true)
{
if (ap.List.Rows.Count != 0)
{
customerDataRow.RetMesg = "该商户ID已存在,请重新输入商户ID";
customerDataRow.CodeIsValid = false;
return customerDataRow;
}
}
}

if (sTp == "2")
{
if (System.Convert.ToDouble(sTxt) < 1 || System.Convert.ToDouble(sTxt) > 7)
{
customerDataRow.RetMesg = "只能输入(1-7)数字";
customerDataRow.CodeIsValid = false;
}
else
customerDataRow.CodeIsValid = true;
}

if (sTp == "3")
{
if (System.Convert.ToDouble(sTxt) < 1 || System.Convert.ToDouble(sTxt) > 30)
{
customerDataRow.RetMesg = "只能输入(1-30)数字";
customerDataRow.CodeIsValid = false;
}
else
customerDataRow.CodeIsValid = true;
}
}
catch
{
customerDataRow.RetMesg = "保存失败";
customerDataRow.CodeIsValid = false;
}
return customerDataRow;
}


52,797

社区成员

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

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