简体   繁体   中英

jQuery.ajax only works once

A part of my chrome extension tests to see if the user is connected to the internet. It works once perfectly but after it runs, $.ajax === undefined and I don't know why. I don't see any setters which could modify $.ajax

var testInternet = function(callback) {
    "use strict";
    var testURLs, doCallback, i, failCount;
    if (! callback) {
        callback = function(){};
    }
    doCallback = true;
    failCount = 0;
    testURLs = [
    "http://clients5.google.com/pagead/drt/dn/dn.js"
    ];
    for (i= 0; i < testURLs.length; i++) {
        testConnection(testURLs[i], function(success){
            if (success && doCallback) {
                doCallback = false;
                callback(true);
                return;
            } else {
                failCount += 1;
                if (failCount === testURLs.length) {
                    callback(false);
                    return;
                }
            }
        });
    }
}

var testConnection = function(url, callback) {
    "use strict";
    $.ajax({
        url: url,
        timeout: 10000,
        cache: false,
        success: function(data) {
            callback(true);
            return;
        },
        error: function(data) {,
            callback(false);
            return;
        }
    });
}

It looks like because you are getting a .js file, jQuery automatically defaults the dataType to script , which will cause jQuery to evaluate the script being loaded. Specifying dataType: 'text' should prevent that unwanted behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM