日期:2014-05-18 浏览次数:20485 次
<script language="javascript" type="text/javascript" src="/JavaScript/jquery.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#Login').click(function() {
$.ajax({
type: "POST", //访问WebService使用Post方式请求
contentType: "application/json;utf-8", //WebService 会返回Json类型
url: "/User/UserLogin.asmx/HelloWorld", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
data: "{}", //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到
dataType: "json",
success: function(result) { //回调函数,result,返回值
try {
alert(result.d);
}
catch (e) {
alert(e)
}
},
error: function(x, e) {
alert("错误");
}
});
});
});
</script>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
namespace Food5u.Web.User
{
/// <summary>
/// UserLogin 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class UserLogin : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}