2012-12-31 44 views
1

我有這樣的谷歌Chrome瀏覽器擴展..AJAX firing no_connection();太早

manifest.json的:

{ 
    "name": "My extension", 
    "manifest_version": 2, 
    "version": "1.0", 
    "permissions": [ 
    "tabs", "http://*/*" 
    ], 
    "browser_action": { 
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
    }, 
    "content_scripts": [ 
    { 
     "matches": ["http://*/*", "https://*/*"], 
     "js": ["jquery-1.8.3.min.js", "content.js"], 
     "run_at": "document_end" 
    } 
    ] 
} 

popup.html:

<!doctype html> 
<html> 
    <head> 
    <title>Getting Started Extension's Popup</title> 
    <style> 
     body { 
     min-width:357px; 
     overflow-x:hidden; 
     } 

     img { 
     margin:5px; 
     border:2px solid black; 
     vertical-align:middle; 
     width:75px; 
     height:75px; 
     } 
    </style> 

    <!-- JavaScript and HTML must be in separate files for security. --> 
    <script src="popup.js"></script> 
    </head> 
    <body> 
    <div id="ajax"> 
    </div> 
    </body> 
</html> 

popup.js:

function start() { 
    var reg = false; 
    if (window.ActiveXObject){ 
     reg = new ActiveXObject("Microsoft.XMLHTTP"); 
    }else { 
     reg = new XMLHttpRequest(); 
    } 
    reg.open("GET","http://www.dr.dk/",true); // Insert a reference of the php page you wanna get instead of yourpage.php 
    reg.send(null); 
    reg.onreadystatechange = function() { 
     if (reg.readyState == 4 && reg.status == 200) { 
      document.getElementById('ajax').innerHTML = reg.responseText; 
     }else { 
      no_connection(); 
     } 
    } 
} 

function no_connection() { 

    var reg = false; 
    if (window.ActiveXObject){ 
     reg = new ActiveXObject("Microsoft.XMLHTTP"); 
    }else { 
     reg = new XMLHttpRequest(); 
    } 
    reg.open("GET","no_connection.html",true); // Insert a reference of the php page you wanna get instead of yourpage.php 
    reg.send(null); 
    reg.onreadystatechange = function() { 
     if (reg.readyState == 4 && reg.status == 200) { 
      document.getElementById('ajax').innerHTML = reg.responseText; 
     }else { 
      document.getElementById('ajax').innerHTML = 'An Unknown Error did happened.'; 
     } 
    } 
} 

start(); 

這只是總是提出來自no_connection.html的內容,但如果我發表評論的話國家統計局:

no_connection(); 

從:

function start(); 

然後正常工作,那麼它顯示的http://www.dr.dk/

如何能發生這種情況,當no_conncection();if else語句中,如何內容那麼它可以重寫嗎?

任何想法如何解決這個問題,因爲這變得非常奇怪。

回答

1

reg.onreadystatechange是一個塊函數,每當你的狀態改變時都會被調用。因此在通話期間以及在通話之後被呼叫。 (兩次,可能更多)

此外,阿里納斯,記得dr.dk是非常憤怒的,當有人從他們的網站內容水蛭,甚至只是他們的鏈接從其他網站...

在你別的聲明,您需要特別傾聽失敗。建議結構:

request[requestid].onreadystatechange = function() { 
    /* This is a slightly confusing part of the script. We don't wait to hear back from the server before we continue 
    with the communicate() function. It sends the request, and if and when the server gets back to us, whatever's 
    specified as request[requestid].onreadystatechange is performed. So, we have to define .onreadystatechange 
    BEFORE we actually make contact with the server. If you're reading this and trying to learn how it works, 
    you may want to take a glance at the last part of the communicate() function first, and then come back here. */ 
    try { 
    /* We use try and catch because Javascript will give an error when we try to access request[requestid].status if the 
    server is down or if the user navigates away from the page. */ 
    if (request[requestid].readyState == 4 && request[requestid].status == 200) { 
    window.clearTimeout(timeout[requestid]); 
    document.body.style.cursor = 'default'; 
    /* 4 = The AJAX Request is complete; 200 = The HTTP server found the data we needed and was able to send it to us. */ 
    eval(request[requestid].responseText); 
    } else if (request[requestid].readyState == 4 && request[requestid].status != 200) { 
    window.clearTimeout(timeout[requestid]); 
    if (failure) eval(failure); 
    document.body.style.cursor = 'default'; 
    alert ('Error ' + request[requestid].status + ': Server error. If you entered data, it may or may not have been saved. Please contact your systems administrator.'); 
    } 
    } catch(e) { 
    window.clearTimeout(timeout[requestid]); 
    document.body.style.cursor = 'default'; 
    if (failure) eval(failure); 
    alert ('Error: Unable to communicate with server. Please contact your systems administrator. You may want to try again in a few minutes to see if the problem fixes itself. \n\n(Either the server was down, the communication was interrupted, or there was an error in the data sent by the server.)\n' + e + '\n\n' + request[requestid].responseText); 
    } 
    } 
+0

謝謝尼爾斯。 dr.dk只是一個例子,在我嘗試製作自己的API來獲取內容之前。 但是,我將如何使它適用於我,然後,對不起,但林新的Javascript。 –

+0

Np。我在回覆帖子中添加了一個建議結構。 –

+0

謝謝,但我只是得到以下錯誤'請求未定義',我應該仍然包括: var request = false; if(window.ActiveXObject){request = new ActiveXObject(「Microsoft.XMLHTTP」); } else {request = new XMLHttpRequest(); }' –