Ajax之后location.href无法工作(更多详细信息)

时间:2018-08-28 06:49:28

标签: javascript jquery ajax window.location

ajax调用成功时,将执行回调函数。回调函数中的函数无法正常工作。

我的代码是:

    sendAjax(url, data, method, async, callBackFn, IgnoreException) {
         $.ajax({
            type:method,
            async:async,
            url:url,
            data:data
            timeout:timeout,
            beforeSend:function(xmlHttpRequest) {
              xmlHttpRequest.setRequestHeader("AJAX", "true");
            },
            success:function(data) {
               try {
                 var responseText = $.parseJSON(data);
                 if (responseText.error_code && !IgnoreException) {
                    alert(responseText.error_msg);
                    var fnName = responseText.LOGIN||{};
                    if (typeof window[fnName] == 'function') {
                       var args = responseText.LOGIN_PARAM||{};
                       window[fnName](args);
                    }
                 } else {
                    callBackFn($.parseJSON(data));
                    return;
                 }
               } catch(e) {
                 ~~~~~~~~~
               } finally {
               }
            }

})

}

。 。 。

sendAjax("mo/serviceAction.do", $("#form").serialize(), "post", true, callback);

。 。 。

function callback(param) {
   alert("Here!");
   location.href = "intent://host&params#intent;scheme='scheme';package=package";
}

但是,如果ajax调用后未执行的功能作为按钮标签上的click事件执行,则会执行 location.href ... 但是,alert(“ Here!”)正在运行!

为什么两种情况不同?

如果我想直接在回调中运行location.href怎么办?

1 个答案:

答案 0 :(得分:1)

您使用acllBackFn作为参数名称时拼写错误。

sendAjax(url, data, method, async, acllBackFn, IgnoreException)

以后。

callBackFn($.parseJSON(data));
相关问题