请求ashx
function GetJPData(domain, action, para, callfun) { $.getJSON(domain + "/Handler.ashx?action=" + action + "&" + para + "&fun=?", callfun); }
$("#btnLogin").click(function () {
var userName = $("#userNameId").val();
var userPwd = $("#userPwdId").val();
var para = "userName=" + userName + "&userPwd=" + userPwd + "";
GetJPData("", "", para, function (data) {
alert(data.userState);
})
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/javascript";
context.Response.Write(Login());
}
public bool IsReusable
{
get
{
return false;
}
}
[WebMethod]
public string Login()
{
string userName = HttpContext.Current.Request["userName"];
string userPwd = HttpContext.Current.Request["userPwd"];
string callfun = HttpContext.Current.Request["fun"];
UserInfo userInfo = new UserInfo();
string userState= userInfo.UserLogin(userName, userPwd);
string userStr="'userState':"+userState;
return callfun + "({" + userStr + "})";
}
请求webservice
function JQAjax(url, vTime) {
var m_Start = function () { isLoaded = false; };
var m_Stop = function () { isLoaded = true; };
var m_Error = function () { isLoaded = true; };
var m_Success = function () { };
var m_Timer = null;
var m_Url = url;
var overTime = 20000; //默认设置20秒超时
if (vTime != null) { overTime = vTime; }
this.OnStart = function (vPara) {
m_Start = vPara;
};
this.OnStop = function (vPara) {
m_Stop = vPara;
};
this.OnError = function (vPara) {
m_Error = vPara;
};
this.OnSuccess = function (vPara) {
m_Success = vPara;
};
this.OnTimeout = function (vPara) {
m_Timer = window.setTimeout(vPara, overTime);
};
this.OnSend = function (sendData, DType, asyc) {
$.ajax({
global: false, //(默认: true) 是否触发全局 AJAX 事件
async: asyc, //异步请求。同步请求请设置为false同步请求将锁住浏览器。
type: "POST", //默认为GET
url: m_Url + "?rnd=" + GetRandomNum(1, 10000),
cache: true, //设置为false将不会从浏览器缓存中加载请求信息
contentType: DType == "json" ? "application/json" : "application/x-www-form-urlencoded;charset=utf-8", //发送信息至服务器时内容编码类型
data: sendData, //发送到服务器的数据
dataType: DType, //xml,html,script,json,jsonp,text
dataFilter: function (data, type) {
return data // 返回处理后的数据
},
beforeSend: function (XMLHttpRequest) {
try { m_Start(); } catch (e) { }
},
complete: function (XMLHttpRequest, textStatus) {
if (m_Timer != null) { window.clearTimeout(m_Timer); }
try { m_Stop(); } catch (e) { }
}, //请求完成后回调函数 (请求成功或失败时均调用)
error: function (XMLHttpRequest, textStatus, errorThrown) {
//清除超时提示,并返回错误类型
if (m_Timer != null) { window.clearTimeout(m_Timer); }
try { m_Error(textStatus); } catch (e) { }
},
success: function (result) {
//清除超时提示,并返回结果
if (m_Timer != null) window.clearTimeout(m_Timer);
try { m_Success(result); } catch (e) { }
}
});
}
}
/*----产生范围内的随机数--------*/
function GetRandomNum(Min, Max) {
var Range = Max - Min;
var Rand = Math.random();
return (Min + Math.round(Rand * Range));
}
var okAjax = new JQAjax("/WebService.asmx/InsertStuInfo");
var _BuySendData = { 'stuNo': 0, 'stuName': 1,'stuScore':3 };
okAjax.OnSuccess(function (result) {
alert(result.d);
}
)
//json请求方式须将参数构造成json字符串 xml直接传输json
// var jsonText = JSON.stringify(_BuySendData);
// alert(jsonText);
//xml方式数据的解析 $(result).find("Code").text()
okAjax.OnSend(_BuySendData, "json", true);