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
語句中,如何內容那麼它可以重寫嗎?
任何想法如何解決這個問題,因爲這變得非常奇怪。
謝謝尼爾斯。 dr.dk只是一個例子,在我嘗試製作自己的API來獲取內容之前。 但是,我將如何使它適用於我,然後,對不起,但林新的Javascript。 –
Np。我在回覆帖子中添加了一個建議結構。 –
謝謝,但我只是得到以下錯誤'請求未定義',我應該仍然包括: var request = false; if(window.ActiveXObject){request = new ActiveXObject(「Microsoft.XMLHTTP」); } else {request = new XMLHttpRequest(); }' –