当IE调试器打开时,jQuery ajax只能在IE中运行 [英] jQuery ajax only works in IE when the IE debugger is open

查看:196
本文介绍了当IE调试器打开时,jQuery ajax只能在IE中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了IE(11)的奇怪问题。我有简单的jQuery代码:

I've faced strange issue with IE (11). I have simple jQuery code:

$("#my-radio").click(function () {
     $.ajax({
        url: "/my-url",
    }).error(function(jqXHR, textStatus, errorThrown) {
        $("#error-summary").text(errorThrown);
    });
});

问题是这个代码在IE以外的所有浏览器中都能很好地工作(在我的情况下除了IE 11,还没有在早期版本中测试)但是,当我打开IE内置的javascript调试器看看有什么问题 - 一切都开始工作正常。 IE对控制器动作进行ajax调用,控制器中的断点被命中。动作不返回任何内容(它的类型为 void ),只计算一些东西。我已经尝试过在这个帖子中提供的插件解决方案但没有用。

The thing is that this code works perfectly in all browsers except IE (in my case except IE 11, didn't test in earlier versions yet) BUT, when I open IE built in javascript debugger to see what's wrong - everything starts to works just fine. IE makes ajax call to controller action, breakpoint in controller gets hit. Action returns nothing (it's of type void), just counting some stuff. I've already tried plugin solution provided in this thread but it wasn't useful.

更新:

为了证明问题我已经发布了简单的MVC应用程序这里。当您单击图像时,浏览器会对此操作发出ajax请求:

To demonstrate the issue I've published simple MVC application here. When you click on image, browser makes ajax request to this action:

public string Hello()
{
     string ip = Request.ServerVariables["REMOTE_ADDR"];
     string agent = Request.UserAgent;    

     var request = new Request
     {
          IP = ip,
          Time = DateTime.UtcNow,
          UserAgent = agent
     };

     _requestsRepository.Add(request);

     int byIp = _requestsRepository.GetTotalRequestsCountByUser(ip);
     int total = _requestsRepository.TotalRequests;

     string message = string.Format("Hello, {0}. This is your {1}th request and {2}th request in total.", ip, byIp, total);

     return message;
 }

这个jQuery代码:

with this jQuery code:

$(document).ready(function () {
    $("#click-me").click(function () {
        $.ajax({
            url: "/services/hello",
        })

        .success(function (data) {
            alert(data);
        })

        .error(function (jqXHR, textStatus, errorThrown) {
            $("#error-summary").text(errorThrown);
        });
    });
});

如您所见 - action返回简单的字符串,该字符串会在成功时发出警报。您可以在IE中尝试此页面。您会注意到请求数量没有增加,因为实际上没有发送请求但仍然成功调用事件处理程序并且警告消息(这可能是一些可能成功的请求之后的一些旧的缓存消息,我不确定)。在任何其他浏览器中,请求数量将增加,因为每次单击图像时都会发送这些请求。如果你打开IE调试器 - 也会发送请求。我建议这是某种与IE缓存相关的问题。

As you can see - action returns simple string which is alerted on success. You can try this page in IE. You will notice that the amount of requests doesn't grow, because NO request is actually sent but still success event handler is called and message is alerted (this is probably some old cached message after some possibly one successful request, I'm not sure). In any other browser the amount of requests will grow, because they are sent each time you click the image. If you turn on IE debugger - requests will be sent too. I suggest this is some kind of IE cache related issue.

推荐答案

听起来你可能会得到缓存的响应。开发人员工具可能启用了始终从服务器刷新选项,导致您只在工具本身打开时获得新的响应。

It sounds like you may be getting cached responses. The developer tools may have the Always Refresh from Server option enabled, causing you to only get a fresh response when the tools themselves are opened.

尝试在 jQuery AJAX选项<中添加缓存选项/ a> object,并将其值设置为 false

Try adding the cache option in the jQuery AJAX options object, and set its value to false.

这篇关于当IE调试器打开时,jQuery ajax只能在IE中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆