/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class GetPrice2 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string sName=context.Request["name"];
//这里是从数据库取得数据 自己写代码实现
var data= new t_productTableAdapter().GetDataByName(sName);
if (data.Count <= 0)
{
context.Response.Write("none|0");
}
else
{
context.Response.Write("ok|"+data.Single().price);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
2、前台调用
<script type="text/javascript">
$(function() {
$("#Text1").blur(function() {
var name = $("#Text1").val();
$.post("GetPrice2.ashx", { "name": name }, function(data, status) {
if (status == "success") {
var arrs = data.split("|");
if (arrs[0] == "ok") {
$("#Text2").val(arrs[1]);
}
else if (arrs[0] == "none") {
$("#Text2").val(arrs[1]);
}
else {
alert('返回数据格式错误');
}
}
else {
alert('ajax错误');
}
});
});
});
</script>