【问题标题】:Ajax call works in IE and Chrome (and only in Firefox when adding breakpoint in debug mode at end of ajax call)Ajax 调用在 IE 和 Chrome 中有效(并且仅在 Firefox 中在 ajax 调用结束时以调试模式添加断点时)
【发布时间】:2016-08-02 16:33:11
【问题描述】:

我目前有以下可在 IE 和 Chrome 中运行的代码。 以下问题仅存在于 Firefox 中(奇怪的是,在 Debug 模式下我可以让它在 Firefox 中工作,并且我在下面的 cmets 所示的 ajax 调用末尾添加了一个断点)。我相信这可能与下面的 ajax 调用有关(它出错的地方)。 (详见代码注释)。

我有以下代码从表单接收数据并将其发送到另一个 JavaScript 函数以将数据发送回 MVC 控制器函数,该函数将用户添加到数据库中。

$('#addUserSubmit').click(function () {
    var tagId = $(this).closest('form').find('input[name="userTag"]').val();
    var adminUser = $(this).closest('form').find('input[name="userName"]').val();
    AddUser(tagId, adminUser);
});

function AddUser(tagId, adminUser) {
    var data = JSON.stringify({
        'tagId': tagId,
        'adminUser': adminUser
    });
    $.ajax({ //HERE IS WHERE THE ERROR OCCURS (After it goes through to set all of the variables below).
        type: "POST",
        url: "/UsersAccounts/AddUser",
        data: data,
        success: function (outVal) {
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest + textStatus + errorThrown);
        },
        contentType: 'application/json'
    }); //IF I ADD A BREAKPOINT HERE IT WILL WORK IN GOING TO THE MVC 
        //FUNCTION TO BELOW AND IT WILL WORK AS INTENDED (BUT IF I RUN 
        //THE CODE NORMALLY OR WITHOUT A BREAKPOINT HERE IT WILL ALERT 
        //WITH THE ERROR ABOVE).
}

    [HttpPost]
    public String AddUser(int tagId, string adminUser){
        if (tagId > 0){
            try{
                using (TubeBPC db = new TubeBPC()){
                    Users user = new Users() {TAGID=tagId, AdminUser=adminUser };
                    db.Users.Add(user);
                    db.SaveChanges();
                    return "success";
                }
            }
            catch(Exception dbe){
                throw dbe;
            }
        }
        else{
            throw new ArgumentOutOfRangeException("TagID is not a positive number");
        }
     }

非常感谢任何帮助!

【问题讨论】:

    标签: javascript jquery ajax firefox model-view-controller


    【解决方案1】:

    我设法进行了更多研究,发现这个答案就是解决方案。

    “为了将此代码包装在一个函数中,返回一个布尔值并且不需要回调函数(阻塞过程),您需要使用同步请求”

    jquery ajax dont work without firebug break point

    希望对其他人有所帮助!

    【讨论】:

    • 不幸的是,我看不出它如何成为解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2012-04-06
    • 2016-07-23
    • 1970-01-01
    • 2017-11-07
    • 2014-05-24
    相关资源
    最近更新 更多