dongtu4028 2015-01-21 00:18
浏览 86

jQuery ajax函数无法正常工作没有请求发送并且没有收到错误

I have some code which I am using to call $.ajax

function doajax(url, dataType, data, method, context, callback, block) {
    block = typeof block !== 'undefined' ? block : true;
    if (block) {
        showOverlay();
    }
    var settings = {
        dataType: dataType,
        data: data,
        type: method,
        url: url,
        error: onerror,
        success: function (data, textStatus, jqXHR) { onsuccess(data, textStatus, jqXHR, context, callback); }
    };
    $.ajax(url, settings);
    console.log("doajax: called $.ajax");
    return false;
}

function onerror(jqXHR, textStatus, errorThrown) {
    console.log("doajax: error");
    hideOverlay();
    formSpinner.stop();
    var extra = jqXHR.hasOwnProperty("responseJSON") ? jqXHR.responseJSON.error : jqXHR.responseText;
    alert("Error (" + jqXHR.status + ") :: " + textStatus + " :: " + errorThrown + " :: " + extra);
}

function onsuccess(data, textStatus, jqXHR, context, callback) {
    console.log("doajax: success");
    hideOverlay();
    formSpinner.stop();
    if (data.status === 200 || data.status === 201) {
        hooks[callback](data, context);
    }
    else {
        alert("Error (" + jqXHR.status + ") :: " + textStatus + " :: " + data.error);
    }
}

Everything was working ok, but now, there is no request received by the server, and the Chrome network window is showing me a GET 200 OK regardless of whether it was a GET or POST request.

In the network tab, there is NO request which goes out for the url: url parameter from the $.ajax settings, instead I am seeing a request for the current page, which produces the 200 OK response.

There is no error being thrown, and the success function is also never called.

I have been looking at possible caching issues, however, POST requests are never cached so I am unsure what is happening.

EDIT: Added console.log() and after performing a UI function which should trigger the $.ajax() call the log is showing:

doajax: called $.ajax

I have also turned on Log XMLHttpRequests and I am seeing confirmation that the request does in fact generate but for the current page url, not the parameter passed in via the settings

Answer: This is an issue with jQuery versions. I found there is an old version of jQuery being used where I have just integrated this code. When I drop jQuery 1.11 in on top of my code, it is all working ok.

So the actual problem is that until jQuery 1.5 there was no support for $.ajax(url, settings); and only $.ajax(settings); can be used.

I can confirm that both using jQuery 1.5+ or changing my code to be $.ajax(settings); will work and fix the problem.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器