eg:
$.ajax({
type: "Post", //要用post方式
url: "../Ajax/WebServiceHandler.aspx/UserLogin", //预警
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{"strLoginName":"' + loginName + '","strLoginPwd":"' + loginPwd + '"}',
success: function (res) {
if (res.d == "fail") {
$(".divLoginError").html("输入的信息有误,请重新输入!");
}
else {
$("#btnLogin").html("欢迎您:<br/>" + res.d);
$(".divLoginShadow").css("display", "none");
$("#divLogin").css("display", "none");
loginIndex = 1;
dialogIndex = 1;
}
},
error: function (err) {
}
});
(1)在VS2005中,Success返回的结果直接就是data,不需要data.d
eg:
jQuery_1_7_0.ajax({
type: "POST", //要用post方式
url: "WebServiceFH.asmx/ShowSomeStatisticalData",
contentType: "application/json",
data: {},
dataType: "json",
success: function (data) {
jQuery_1_7_0("#lblRealPower").text(data["RealPower"]);
jQuery_1_7_0("#lblRealLoad").text(data["RealLoad"]);
jQuery_1_7_0("#lblElectricityByDay").text(data["ElectricityByDay"]);
jQuery_1_7_0("#lblElectricityByMonth").text(data["ElectricityByMonth"]);
jQuery_1_7_0("#lblElectricityByYear").text(data["ElectricityByYear"]);
jQuery_1_7_0("#lblPowerByDay").text(data["PowerByDay"]);
jQuery_1_7_0("#lblPowerByMonth").text(data["PowerByMonth"]);
jQuery_1_7_0("#lblPowerByYear").text(data["PowerByYear"]);
var strRealLoadByOne=0;
var strRealLoadByTwo=0;
if(Number(data["RealLoadByOne"])>=0){
strRealLoadByOne=Number(data["RealLoadByOne"]);
}
if(Number(data["RrealLoadByTwo"])>=0){
strRealLoadByTwo=Number(data["RrealLoadByTwo"]);
}
jQuery_1_7_0("#CircularStatistics1").html("<input type=\"text\" class=\"CrewOne\" data-fgColor=\"orange\" data-thickness=\".2\" value='" + strRealLoadByOne + "' data-readOnly=\"true\"/>");
jQuery_1_7_0("#CircularStatistics2").html("<input type=\"text\" class=\"CrewTwo\" data-fgColor=\"blue\" data-thickness=\".2\" value='" + strRealLoadByTwo + "' data-readOnly=\"true\"/>");
//机组1负荷
jQuery_1_7_0(".CrewOne").knob();
//机组2负荷
jQuery_1_7_0(".CrewTwo").knob();
},
error: function (err) {
alert("读取数据出错!");
}
});
注意:jQuery_1_7_0相当于“$”
(2)ajax调用webservice 中的方法时,
1、webservice 类名前面一定要加“[System.Web.Script.Services.ScriptService]”;
2、方法前面一定要加“[WebMethod]”
eg:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using SJTSoft.SSO.BLL;
using SJTSoft.SSO.Model;
/// <summary>
/// WebServiceFH 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebServiceFH : System.Web.Services.WebService
{
public WebServiceFH()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetBurden(String strCode)
{
String strReturn = "";
try
{
strReturn = new SissService().GetBurdenOfWebservice(strCode);
}
catch (Exception er)
{
strReturn = "-1";
}
return strReturn;
}
/// <summary>
/// 页面数据
/// </summary>
/// <returns></returns>
[WebMethod]
public IndexStatisticalData ShowSomeStatisticalData()
{
IndexStatisticalData tempISD = new IndexStatisticalData();
SISService sis = new SISService();
tempISD.RealPower = sis.GetRealPower();
tempISD.RealLoad = sis.GetRealLoad();
tempISD.ElectricityByDay = sis.GetElectricityByDay();
tempISD.ElectricityByMonth = sis.GetElectricityByMonth();
tempISD.ElectricityByYear = sis.GetElectricityByYear();
tempISD.PowerByDay = sis.GetPowerByDay();
tempISD.PowerByMonth = sis.GetPowerByMonth();
tempISD.PowerByYear = sis.GetPowerByYear();
tempISD.RealLoadByOne = sis.GetRealLoadByOne();
tempISD.RrealLoadByTwo = sis.GetRealLoadByTwo();
return tempISD;
}
/// <summary>
/// 1号机组24小时负荷
/// </summary>
/// <returns></returns>
[WebMethod]
public LoadEveryHour GetLoadEveryHourByOne()
{
LoadEveryHour tempLEH = new LoadEveryHour();
SISService sis = new SISService();
tempLEH = sis.GetLoadEveryHourByOne();
return tempLEH;
}
/// <summary>
/// 2号机组24小时负荷
/// </summary>
/// <returns></returns>
[WebMethod]
public LoadEveryHour GetLoadEveryHourByTwo()
{
LoadEveryHour tempLEHTwo = new LoadEveryHour();
SISService sisTwo = new SISService();
tempLEHTwo = sisTwo.GetLoadEveryHourByTwo();
return tempLEHTwo;
}
}
注:IndexStatisticalData和LoadEveryHour都是对象哦!