2017-04-07 95 views
1

我一直在寻找通过使用没有onclick函数的ajax获取数据的例子,但是我发现的大部分例子都在onclick函数中。
这里是我成功使用的onclick funtion用户ajax在javaScript中没有“onClick函数”的情况下获取数据

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>New document</title> 

    <script type = 'text/javascript' src='js/jquery-2.2.0.js'></script> 

<script type ='text/javascript' src='js/testScript.js'></script> 

<?php 
include_once("database_conn_getOffers.php"); 
?> 
</head> 
<body> 
    content goes here 
    <aside id="offer" onclick ="loadDoc('getData.php',myFunction)"> 
     &nbsp; click here 
     </aside> 
</body> 

获取数据的HTML这里是脚本

function loadDoc(url, cFunction) { 
    var xhttp; 
    xhttp=new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     cFunction(this); 
    } 
    }; 
    xhttp.open("GET", url, true); 
    xhttp.send(); 
} 
function myFunction(xhttp) { 
    document.getElementById("offer").innerHTML = 
    xhttp.responseText; 
} 

现在,我想获得的数据没有的onclick功能。
我希望数据显示一旦我打开html。
有人可以提供一些链接或gv我的例子?

+0

为什么不将函数调用移出onclick并在

1

所有你需要做的就是调用函数的其他地方,例如你可以将它设置为在窗口加载时调用。

Window.onload = loadDoc('getData.php',myFunction)

1

您可以使用

<body onload="loadDoc('getData.php',myFunction);">

1

第三种解决办法是增加一个新的DOMContentLoaded事件来发送浏览器后呈现页面查询。

应该是这样的:需要

document.addEventListener('DOMContentLoaded', loadDoc);

1

已经绑定Ajax调用的事件onclick,你想要的是在页面加载。 所以更换的onclick到onload

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>New document</title> 

    <script type = 'text/javascript' src='js/jquery-2.2.0.js'></script> 

    <script type ='text/javascript' src='js/testScript.js'></script> 

<?php 
include_once("database_conn_getOffers.php"); 
?> 
</head> 
<body> 
    content goes here 
    <aside id="offer" onload ="loadDoc('getData.php',myFunction)"> 
    &nbsp; click here 
    </aside> 
</body> 

1

下面的代码将理想的工作,如果同一个域的页面。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="utf-8" /> 
 
    <title>New document</title> 
 

 
    <script type = 'text/javascript' src='js/jquery-2.2.0.js'></script> 
 

 
<script type ='text/javascript' src='js/testScript.js'></script> 
 
<script type ='text/javascript'> 
 
$(document).ready(function(){ 
 
$("#offer").load('getData.php'); 
 
}); 
 
</script> 
 

 
<?php 
 
include_once("database_conn_getOffers.php"); 
 
?> 
 
</head> 
 
<body> 
 
    content goes here 
 
    <aside id="offer" > 
 
     &nbsp; click here 
 
     </aside> 
 
</body>

1

window.onload事件中调用你的函数,而不是onclick

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>New document</title> 

    <script type = 'text/javascript' src='js/jquery-2.2.0.js'></script> 

<script type ='text/javascript' src='js/testScript.js'></script> 

<?php 
include_once("database_conn_getOffers.php"); 
?> 
</head> 
<body> 
    content goes here 
    <aside id="offer"> 
     Your function has been executed onload of document 
     </aside> 
</body> 

这里是脚本

window.onload = function loadDoc(url, cFunction) { 
    var xhttp; 
    xhttp=new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     cFunction(this); 
    } 
    }; 
    xhttp.open("GET", url, true); 
    xhttp.send(); 
} 
function myFunction(xhttp) { 
    document.getElementById("offer").innerHTML = 
    xhttp.responseText; 

}; 
相关问题