Heim > Web-Frontend > js-Tutorial > jQuery AJax ruft den Implementierungscode des asp.net-Webservers auf

jQuery AJax ruft den Implementierungscode des asp.net-Webservers auf

高洛峰
Freigeben: 2017-01-12 14:23:42
Original
992 Leute haben es durchsucht

Aspx-Seitencode

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

 <title></title>

  <script src="JQUERY.JS" type="text/javascript"></script>

  <style type="text/css"><!--

.hover

{

cursor: pointer; /*小手*/

background: #ffc; /*背景*/

}

.button

{

width: 150px;

float: left;

text-align: center;

margin: 10px;

padding: 10px;

border: 1px solid #888;

}

#dictionary

{

text-align: center;

font-size: 18px;

clear: both;

border-top: 3px solid #888;

}

#loading

{

border: 1px #000 solid;

background-color: #eee;

padding: 20px;

margin: 100px 0 0 200px;

position: absolute;

display: none;

}

  

--></style><style type="text/css" bogus="1"><!--

.hover

{

cursor: pointer; /*小手*/

background: #ffc; /*背景*/

}

.button

{

width: 150px;

float: left;

text-align: center;

margin: 10px;

padding: 10px;

border: 1px solid #888;

}

#dictionary

{

text-align: center;

font-size: 18px;

clear: both;

border-top: 3px solid #888;

}

#loading

{

border: 1px #000 solid;

background-color: #eee;

padding: 20px;

margin: 100px 0 0 200px;

position: absolute;

display: none;

}

  

--></style><style type="text/css" bogus="1" bogus="1">.hover

{

cursor: pointer; /*小手*/

background: #ffc; /*背景*/

}

.button

{

width: 150px;

float: left;

text-align: center;

margin: 10px;

padding: 10px;

border: 1px solid #888;

}

#dictionary

{

text-align: center;

font-size: 18px;

clear: both;

border-top: 3px solid #888;

}

#loading

{

border: 1px #000 solid;

background-color: #eee;

padding: 20px;

margin: 100px 0 0 200px;

position: absolute;

display: none;

}

  </style>

  <script type="text/javascript"><!--

    //无参数调用

    $(document).ready(function() {

      $(&#39;#btn1&#39;).click(function() {

        $.ajax({

          type: "POST",  //访问WebService使用Post方式请求

          contentType: "application/json", //WebService 会返回Json类型

          url: "WebService1.asmx/HelloWorld", //调用WebService的地址和方法名称组合 ---- WsURL/方法名

          data: "{}",     //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到   

          dataType: &#39;json&#39;,

          success: function(result) {   //回调函数,result,返回值

            $(&#39;#dictionary&#39;).append(result.d);

          }

        });

      });

    });

    //有参数调用

    $(document).ready(function() {

      $("#btn2").click(function() {

        $.ajax({

          type: "POST",

          contentType: "application/json",

          url: "WebService1.asmx/GetWish",

          data: "{value1:&#39;心想事成&#39;,value2:&#39;万事如意&#39;,value3:&#39;牛牛牛&#39;,value4:2009}",

          dataType: &#39;json&#39;,

          success: function(result) {

            $(&#39;#dictionary&#39;).append(result.d);

          }

        });

      });

    });

    

    

    //返回集合(引用自网络,很说明问题)

    $(document).ready(function() {

      $("#btn3").click(function() {

        $.ajax({

          type: "POST",

          contentType: "application/json",

          url: "WebService1.asmx/GetArray",

          data: "{i:10}",

          dataType: &#39;json&#39;,

          success: function(result) {

            $(result.d).each(function() {

              //alert(this);

              $(&#39;#dictionary&#39;).append(this.toString() + " ");

              //alert(result.d.join(" | "));

            });

          }

        });

      });

    });

    //返回复合类型

    $(document).ready(function() {

      $(&#39;#btn4&#39;).click(function() {

        $.ajax({

          type: "POST",

          contentType: "application/json",

          url: "WebService1.asmx/GetClass",

          data: "{}",

          dataType: &#39;json&#39;,

          success: function(result) {

            $(result.d).each(function() {

              //alert(this);

              $(&#39;#dictionary&#39;).append(this[&#39;ID&#39;] + " " + this[&#39;Value&#39;]);

              //alert(result.d.join(" | "));

            });

          }

        });

      });

    });

    //返回DataSet(XML)

    $(document).ready(function() {

      $(&#39;#btn5&#39;).click(function() {

        $.ajax({

          type: "POST",

          url: "WebService1.asmx/GetDataSet",

          data: "{}",

          dataType: &#39;xml&#39;, //返回的类型为XML ,和前面的Json,不一样了

          success: function(result) {

          //演示一下捕获

            try { 

              $(result).find("Table1").each(function() {

                $(&#39;#dictionary&#39;).append($(this).find("ID").text() + " " + $(this).find("Value").text());

              });

            }

            catch (e) {

              alert(e);

              return;

            }

          },

          error: function(result, status) { //如果没有上面的捕获出错会执行这里的回调函数

            if (status == &#39;error&#39;) {

              alert(status);

            }

          }

        });

      });

    });

    //Ajax 为用户提供反馈,利用ajaxStart和ajaxStop 方法,演示ajax跟踪相关事件的回调,他们两个方法可以添加给jQuery对象在Ajax前后回调

    //但对与Ajax的监控,本身是全局性的

    $(document).ready(function() {

      $(&#39;#loading&#39;).ajaxStart(function() {

        $(this).show();

      }).ajaxStop(function() {

        $(this).hide();

      });

    });

    // 鼠标移入移出效果,多个元素的时候,可以使用“,”隔开

    $(document).ready(function() {

      $(&#39;div.button&#39;).hover(function() {

        $(this).addClass(&#39;hover&#39;);

      }, function() {

        $(this).removeClass(&#39;hover&#39;);

      });

    });

    

    

  

// --></script>

</head>

<body>

  <form id="form1" runat="server">

  <div id="switcher">

    <h2>

      jQuery 的WebServices 调用</h2>

    <div class="button" id="btn1">

      HelloWorld</div>

    <div class="button" id="btn2">

      传入参数</div>

    <div class="button" id="btn3">

      返回集合</div>

    <div class="button" id="btn4">

      返回复合类型</div>

    <div class="button" id="btn5">

      返回DataSet(XML)</div>

  </div>

  <div id="loading">

    服务器处理中,请稍后。

  </div>

  <div id="dictionary">

  </div>

  </form>

</body>

</html>

Nach dem Login kopieren

WebService1.asmx-Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

using System.Collections.Generic;

namespace jquery_Learning

{

/// <summary>

/// WebService1 的摘要说明

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

[System.Web.Script.Services.ScriptService]

public class WebService1 : System.Web.Services.WebService

{

/// <summary>

/// 无参数

/// </summary>

/// <returns></returns>

[WebMethod]

public string HelloWorld()

{

return "Hello World ";

}

/// <summary>

/// 带参数

/// </summary>

/// <param name="value1"></param>

/// <param name="value2"></param>

/// <param name="value3"></param>

/// <param name="value4"></param>

/// <returns></returns>

[WebMethod]

public string GetWish(string value1, string value2, string value3, int value4)

{

return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4);

}

/// <summary>

/// 返回集合

/// </summary>

/// <param name="i"></param>

/// <returns></returns>

[WebMethod]

public List<int> GetArray(int i)

{

List<int> list = new List<int>();

while (i >= 0)

{

list.Add(i--);

}

return list;

}

/// <summary>

/// 返回一个复合类型

/// </summary>

/// <returns></returns>

[WebMethod]

public Class1 GetClass()

{

return new Class1 { ID = "1", Value = "牛年大吉" };

}

/// <summary>

/// 返回XML

/// </summary>

/// <returns></returns>

[WebMethod]

public DataSet GetDataSet()

{

DataSet ds = new DataSet();

DataTable dt = new DataTable();

dt.Columns.Add("ID", Type.GetType("System.String"));

dt.Columns.Add("Value", Type.GetType("System.String"));

DataRow dr = dt.NewRow();

dr["ID"] = "1";

dr["Value"] = "新年快乐";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr["ID"] = "2";

dr["Value"] = "万事如意";

dt.Rows.Add(dr);

ds.Tables.Add(dt);

return ds;

}

}

//自定义的类,只有两个属性

public class Class1

{

public string ID { get; set; }

public string Value { get; set; }

}

}

Nach dem Login kopieren

Weitere Artikel zum Implementierungscode von jQuery AJax, der asp.net-Webserver aufruft, finden Sie unter PHP chinesische Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage