webservice代码:
1
public
string
GetStr(
int
flag)
2
{
3
if(flag==0)
4
return "http://www.cnblogs.com";
5
else
6
return "测试信息";
7
}

2

3

4

5

6

7

JS代码:
只是一个例子,在现在的开发中,我们很少会这样写,而是使用其它的AJAX框架。
1
function
GetUrl()
2
{
3
var URL = "WebService/WebService.asmx/GetStr?flag=0";
4
var browser = navigator.appName;
5
var xmlhttp;
6
var xmlDoc;
7
if(browser == "Microsoft Internet Explorer"){ //IE浏览器
8
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
9
xmlDoc = new ActiveXObject('Microsoft.XMLDOM'); //创建空的XML文档对象
10
}
11
else{ //其它浏览器
12
xmlhttp = new XMLHttpRequest();
13
xmlDoc = document.implementation.createDocument("","",null);
14
}
15
xmlhttp.open("GET",URL, false);
16
xmlhttp.send(null);
17
var res = xmlhttp.ResponseText;
18
xmlDoc.async = false; //关闭异步加载,确保在文档完整加载之前,不会继续执行脚本
19
xmlDoc.load(xmlhttp.responseBody); //加载XML文档
20
var aRowIds = xmlDoc.selectSingleNode("//string").text; //读取string节点内容
21
if(aRowIds.match('^[index.aspx]/S*')==null) //用正则匹配返回的是否有效URL,是则跳转
22
alert(aRowIds);
23
else
24
top.location.href = aRowIds;
25
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
