通过 Jquery 和 ASP.NET 进行 Ajax

发布于 2024-12-10 20:28:34 字数 1242 浏览 0 评论 0原文

执行摘要

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2024-12-17 20:28:34

IHttpHandler 是在 ASP.net 中实现 AJAX 处理程序的绝佳选择。它的开销比页面低,并且与 WCF Web 服务之类的服务相比易于设置/安装。虽然您可以制作经典的 .asmx Web 服务,但这应该对您发布的问题没有影响。

还有 context.Response.ContentType = "application/json";是从处理程序的 ProcessRequest 方法内部更改 HttpResponse 的 contenttype 标头的正确技术。

您的问题可能是格式错误的响应。现在的问题是缩小响应范围。

  1. 如果您还没有这样做,请使用 firebug、chrome 开发人员工具、fiddler 或wireshark 获取服务器响应的完整副本。使用正确的技术来更改上面突出显示的内容类型,您说 ajax 成功事件永远不会触发,但您没有让我们知道您是否检查了 HttpResponse 的内容类型标头、响应代码和有效的 json。
  2. 验证内容类型标头实际上已设置为“application/json”。
  3. 仔细检查您的 json 实际上在发送时语法正确
    通过电线。 (记住这必须是 JavaScript 可以“评估的”

    • 放错位置的括号、引号或冒号真的会把事情搞砸)。
  4. 你的 json 中有任何控制字符或 unicode 文本吗?是
    他们正确逃脱了吗?这是 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.

  1. If you have not already done so, grab a full copy the server response using firebug, chrome developer tools, fiddler or wireshark. Using the correct technique for changing the content type highlighted above you say the ajax success event never fires, but you did not let us know if you inspected the HttpResponse for content type header, response code and valid json.
  2. Verify that the content type header was in fact set to "application/json".
  3. Double check that your json is in fact syntactically correct as sent
    over the wire. (Remember this must be "evaluate-able" by JavaScript

    • a misplaced bracket, quote or colon can really muck it up).
  4. Do you have any control characters or unicode text in your json? Are
    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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文