通过 Jquery 和 ASP.NET 进行 Ajax
执行摘要
context.Response.Header.Add("Content-Type", "application/json");
时被忽略
context.Response.ContentType = "application/json";
在中断 jQuery ajax 调用
。 =================================================== ==================
我有一个 IHttpHandler,它返回一些 JSON 数据,并定义为:
void ProcessRequest(HttpContext context)
{
context.Response.Write(/* some JSON */);
}
并使用 jQuery Ajax 方法进行调用,如下所示:
$.get(SOME_URL, SUCCESS);
这工作正常。
但是,如果 ProcessRequest 方法是这样定义的:
void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json"
context.Response.Write(/* some JSON */);
}
它会失败,这意味着 $.get(SOME_URL, SUCCESS);
中的 SUCCESS
函数永远不会触发,但正确的内容仍然存在根据 Fiddler2 返回预期的标头。
另一方面,如果 ProcessRequest 方法是这样定义的:
void ProcessRequest(HttpContext context)
{
context.Response.Header.Add("Content-Type", "application/json");
context.Response.Write(/* some JSON */);
}
一切正常,但 Fiddler2 报告它以“text/html”类型返回。
知道为什么会发生这种情况吗?
注意:URL 没有扩展名,并且应用程序在 IIS 7.5 上运行,因此我想知道问题是否与 IIS MIME 类型有关。
Executive Summary
context.Response.Header.Add("Content-Type", "application/json");
gets ignored while
context.Response.ContentType = "application/json";
breaks jQuery ajax calls.
====================================================================
I have an IHttpHandler that returns some JSON data and is defined as:
void ProcessRequest(HttpContext context)
{
context.Response.Write(/* some JSON */);
}
and called using the jQuery Ajax methods like so:
$.get(SOME_URL, SUCCESS);
This works fine.
However, if the ProcessRequest method is defined thus:
void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json"
context.Response.Write(/* some JSON */);
}
it fails, meaning that the SUCCESS
function in $.get(SOME_URL, SUCCESS);
never fires, yet the correct content still get returned with the expected headers according to Fiddler2.
On the other hand, if the ProcessRequest method is defined thus:
void ProcessRequest(HttpContext context)
{
context.Response.Header.Add("Content-Type", "application/json");
context.Response.Write(/* some JSON */);
}
everything works but Fiddler2 reports that it is being returned as type "text/html".
Any idea why that happens?
NOTE: The URL is extensionless and the application is running on IIS 7.5 so I wonder whether or not the problem is related to the IIS MIME types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IHttpHandler 是在 ASP.net 中实现 AJAX 处理程序的绝佳选择。它的开销比页面低,并且与 WCF Web 服务之类的服务相比易于设置/安装。虽然您可以制作经典的 .asmx Web 服务,但这应该对您发布的问题没有影响。
还有 context.Response.ContentType = "application/json";是从处理程序的 ProcessRequest 方法内部更改 HttpResponse 的 contenttype 标头的正确技术。
您的问题可能是格式错误的响应。现在的问题是缩小响应范围。
通过电线。 (记住这必须是 JavaScript 可以“评估的”
他们正确逃脱了吗?这是 json 的一个常见陷阱,
不正确转义的内容或不正确的编码。您是否使用任何
将字节转换为字符串或将字符串转换为字节的代码
代码?如果是这样,请确保您使用的是所需的编码,可能是
utf-8。
如果您怀疑您选择的 UrlRewriter 技术正在更改响应标头,我希望这相对容易关闭并使用基于扩展的 URL 到您的处理程序进行测试?这至少可以确认您正在诊断正确的问题,并且值得您花时间进一步研究该问题。如果您确认 UrlRewriter 是问题所在,您应该发布有关您正在使用的特定 UrlRewriter 技术的问题。例如,您是否使用 Helicon Tech 的 ISAPI Reqwrite 之类的东西?或者微软新的 URL 重写器?您自己的专有解决方案?
An IHttpHandler is an excellent choice for implementing AJAX handlers in ASP.net. It has lower overhead than a page and is easy to setup/install compared to something like WCF web services. While you could make a classic .asmx webservice, this should have no effect on the problem you posted.
Also context.Response.ContentType = "application/json"; is the correct technique for changing the contenttype header of the HttpResponse from inside the ProcessRequest method of your handler.
Your problem will likely be a malformed response. The question is now to narrow down what is breaking the response.
over the wire. (Remember this must be "evaluate-able" by JavaScript
they properly escaped? This is a common pitfall with json,
improperly escaped content or incorrect encoding. Are you using any
code to convert from bytes to strings or string to bytes in your
code? If so, be sure you are using the desired encoding, probably
utf-8.
If you suspect that your chosen UrlRewriter technique is changing the response headers, I would hope this is relatively easy to turn off and test with an extension based URL to your handler? This would at least confirm that you are diagnosing the correct issue and that it is worth your time to further research the issue. If you confirm that the UrlRewriter is the problem, you should post a question about the specific UrlRewriter technology you are using. For example are you using something like Helicon Tech's ISAPI Reqwrite? Or Microsoft's new URL Rewriter? Your own proprietary solution?