62,074
社区成员
发帖
与我相关
我的任务
分享
<head>
<script src="../JS/jquery-1.7.2.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$.ajax({
type: "POST",
url: "Demo.ashx",
dataType: "text",
async: true,
cache: false,
data: { name: 'test' },
success: function(data) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('当你狂刷新页面的时候就弹出来了');
}
});
</script>
</body>
</html>
<%@ WebHandler Language="C#" Class="Demo" %>
using System;
using System.Web;
public class Demo : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("1");
}
public bool IsReusable
{
get
{
return false;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CallBackAndPostBack.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "Handler1.ashx",
async: true,
cache: false,
data: { name:'name'},
success: function (data) {
alert(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('当你狂刷新页面的时候就弹出来了');
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CallBackAndPostBack
{
/// <summary>
/// Summary description for Handler1
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string _name= context.Request["name"].ToString();
context.Response.Write("Hello World"+_name);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
这样的刷到死都不会报错呀 ,但是原因在哪里这个我还没有找出来。
context.Response.ContentType = "text/plain";
string ss = context.Request.Params["name"];//接受一下提交值
context.Response.Write("1");
POST 修改成 GET
<head>
<script src="../JS/jquery-1.7.2.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$.ajax({
type: "POST",
url: "Demo.ashx?name=test",
dataType: "text",
async: true,
cache: false,
success: function(data) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('当你狂刷新页面的时候就弹出来了');
}
});
</script>
</body>
</html>