weixin_33674976 2018-04-04 10:46 采纳率: 0%
浏览 33

AJAX网址无法在IE中使用

I am hoping someone can help me with this. I have only just started using AJAX and I am having an issue with the URL. The user selects an option from a multiselect dropdown list. When they press save, the data is posted to the Ajax below.

 $("#getSelected").click(function (e) {
                     e.preventDefault();
                    var myList  = $('select#my-select').val();
                    var list_new = "[" +myList+ "]"
                    var parseList = JSON.parse(list_new);

                    var ajaxResponse = $.ajax({
                        type: 'POST',
                        url: "/mysite/my-account/updatefavourite.cfm?q=1&buster='+new Date().getTime();",
                        contentType: "application/json; charset=utf-8",
                        cache: false,
                        data: JSON.stringify( parseList ),
                        success: function() {
                               $("#successMessage").show();
                                }
                    })
                    alert("test");
                   location.reload();
                });

In Chrome and firefox this works fine without the alert. In IE, if I include the alert("test") then everything works fine. the data is posted to my page, it updates the database, reloads the page and updates the display list. Once I remove the alert, the url no longer works and the data is not sent to my page. Can someone help. Why in IE it works with the alert but not without it and how I can I get it to work without the alert.

  • 写回答

1条回答 默认 最新

  • weixin_33724659 2018-04-04 10:59
    关注

    The problem is because you're calling reload() before the AJAX request completes, therefore, depending on exactly how fast the response comes back and is handled by the browser, the request is cancelled and the page is unloaded.

    To fix this call reload() within the success handler of the AJAX call:

    $("#getSelected").click(function(e) {
      e.preventDefault();
      // vars...
    
      var ajaxResponse = $.ajax({
        // ajax settings...
        success: function() {
          $("#successMessage").show();
          location.reload();
        }
      });
    });

    That being said, it's very odd to make an AJAX request and then redirect the page. The entire point of AJAX is that you don't have to do that. I'd suggest you amend your logic to do either one or the other only, ie. send all the data in the request to load the page, or send the AJAX request then use JS to amend the DOM as needed with the new information.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示