请问如何让ashx处理程序响应options请求

首先我想实现的目的是让ashx处理跨域请求,请求的ajax如下:
$.ajax({
type: "get",
url: "http://localhost:8011/Sys_Menu.ashx?type=list&r=" + Math.random(),
dataType: "json",
async: false,
beforeSend: function(xhr) {
xhr.setRequestHeader("withCredentials", true);
},
success: function(data) {
alert("true");
},
error: function(a, b, c) {
alert("false")
}
});

Sys_Menu.ashx的程序如下:
public void ProcessRequest(HttpContext context)
{
if (context.Request.Headers["origin"] != null)
{
context.Response.AddHeader("Access-Control-Allow-Origin", context.Request.Headers["origin"]);
}
else
{
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
}
context.Response.AddHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT");
context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
context.Response.ContentType = "application/json";

ResultMes rMes = new ResultMes(true);
context.Response.Write(rMes.ToString());
}

不管我怎么改变Sys_Menu.ashx的返回http状态码,ajax的预请求(OPTIONS)返回状态都为200,如下图,所以我怀疑这个OPTIONS请求根本没有到达Sys_Menu.ashx处理程序中,求各位大神帮忙分析。
...全文
379 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2017-11-30
  • 打赏
  • 举报
回复
刚才type复制错了。使用的是options。图中可以看出来
引用 4 楼 wggfcusmq 的回复:
你的都是get请求呢,没有options请求

$.ajax({
type: "options",
url: "../Ashx/Handler1.ashx",
dataType: "json",
async: false,
success: function (data) {
alert("true");
}
});




  • 打赏
  • 举报
回复
引用 3 楼 hanjun0612 的回复:
[quote=引用 2 楼 wggfcusmq 的回复:] [quote=引用 1 楼 hanjun0612 的回复:] 你返回的只是自己实体类的东西。没有改变状态码啊。 context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
我有改过的,反复调试过大半天,粘上来的不是调试时的代码[/quote] 那你是不是接口地址写错了? 我这边调试毫无压力啊。
public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string a = "{\"total\": 1,\"rows\":[{\"PT_ID\":1,\"PT_ParentID\":0,\"PT_Name\":\"鞋子\",\"PT_Code\":\"Shoes\",\"CompanyInfo_ID\":null,\"PT_CreateTime\":null},{\"PT_ID\":2,\"PT_ParentID\":0,\"PT_Name\":\"dfaz\",\"PT_Code\":\"asfaf\",\"CompanyInfo_ID\":null,\"PT_CreateTime\":null}]}";

            context.Response.StatusCode = (int)HttpStatusCode.BadRequest;

            context.Response.Write(a);
        }
public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string a = "{\"total\": 1,\"rows\":[{\"PT_ID\":1,\"PT_ParentID\":0,\"PT_Name\":\"鞋子\",\"PT_Code\":\"Shoes\",\"CompanyInfo_ID\":null,\"PT_CreateTime\":null},{\"PT_ID\":2,\"PT_ParentID\":0,\"PT_Name\":\"dfaz\",\"PT_Code\":\"asfaf\",\"CompanyInfo_ID\":null,\"PT_CreateTime\":null}]}";

            context.Response.StatusCode = (int)HttpStatusCode.OK;

            context.Response.Write(a);
        }
[/quote] 你的都是get请求呢,没有options请求
正怒月神 版主 2017-11-30
  • 打赏
  • 举报
回复
引用 2 楼 wggfcusmq 的回复:
[quote=引用 1 楼 hanjun0612 的回复:]
你返回的只是自己实体类的东西。没有改变状态码啊。
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;


我有改过的,反复调试过大半天,粘上来的不是调试时的代码[/quote]
那你是不是接口地址写错了?

我这边调试毫无压力啊。
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string a = "{\"total\": 1,\"rows\":[{\"PT_ID\":1,\"PT_ParentID\":0,\"PT_Name\":\"鞋子\",\"PT_Code\":\"Shoes\",\"CompanyInfo_ID\":null,\"PT_CreateTime\":null},{\"PT_ID\":2,\"PT_ParentID\":0,\"PT_Name\":\"dfaz\",\"PT_Code\":\"asfaf\",\"CompanyInfo_ID\":null,\"PT_CreateTime\":null}]}";

context.Response.StatusCode = (int)HttpStatusCode.BadRequest;

context.Response.Write(a);
}




public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string a = "{\"total\": 1,\"rows\":[{\"PT_ID\":1,\"PT_ParentID\":0,\"PT_Name\":\"鞋子\",\"PT_Code\":\"Shoes\",\"CompanyInfo_ID\":null,\"PT_CreateTime\":null},{\"PT_ID\":2,\"PT_ParentID\":0,\"PT_Name\":\"dfaz\",\"PT_Code\":\"asfaf\",\"CompanyInfo_ID\":null,\"PT_CreateTime\":null}]}";

context.Response.StatusCode = (int)HttpStatusCode.OK;

context.Response.Write(a);
}


  • 打赏
  • 举报
回复
引用 1 楼 hanjun0612 的回复:
你返回的只是自己实体类的东西。没有改变状态码啊。 context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
我有改过的,反复调试过大半天,粘上来的不是调试时的代码
  • 打赏
  • 举报
回复
引用 5 楼 hanjun0612 的回复:
刚才type复制错了。使用的是options。图中可以看出来
谢谢,那我再研究研究
lescper2011 2017-11-30
  • 打赏
  • 举报
回复
获取到头部信息,或者作为一个参数发出
正怒月神 版主 2017-11-29
  • 打赏
  • 举报
回复
你返回的只是自己实体类的东西。没有改变状态码啊。 context.Response.StatusCode = (int)HttpStatusCode.BadRequest;

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧