可以使用参数从html调用loadXMLDoc()吗?(ajax) [英] can I use parameter to call loadXMLDoc() from html? (ajax)
本文介绍了可以使用参数从html调用loadXMLDoc()吗?(ajax)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Ajax通话.现在,我需要对其进行参数化,因为同一Ajax可以在服务器上使用所有不同的脚本.
I have working Ajax call. Now I need to parametrize it as the same Ajax could all different scripts on the server.
我需要
- 在
- 运行不同脚本;
- 名称 div 用html表示,以便ajax将更新正确的td
xmlhttp.open("GET","//ajaxrun?run = login_build",true);中
- run different scripts in
xmlhttp.open("GET","/ajaxrun?run=login_build",true);
- name div's in html so the ajax will update correct td
在Ajax中怎么做?
我以为
- 我会像
< button onclick ='loadXMLDoc("login_build")'type ='button'>
这样的方式调用loadXMLDoc,然后 - 将函数声明更改为
function loadXMLDoc(script,function()
和 - 将get调用更改为
xmlhttp.open("GET","/ajaxrun?run =" + script,true);
并最终 - 将div更改为
document.getElementById(script).innerHTML = xmlhttp.responseText;
- I would call loadXMLDoc like
<button onclick='loadXMLDoc("login_build")' type='button'>
and then - change the function declaration to
function loadXMLDoc(script, function()
and - change the get call to
xmlhttp.open("GET","/ajaxrun?run="+script,true);
and finally - change the div to
document.getElementById(script).innerHTML=xmlhttp.responseText;
但是它什么也没做
我的html
<script type='text/javascript'>
//<![CDATA[
function loadXMLDoc()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
document.getElementById("run").innerHTML=xmlhttp.readyState;
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("run").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/ajaxrun?run=login_build",true);
xmlhttp.send();
}
//]]>
</script>
</head>
<body>
<h1>Available test suits</h1>
<br/><br/>
<table>
<tr>
<td>
<a href='run?run=login_build'>login_build</a>
</td>
<td>
<button onclick='loadXMLDoc()' type='button'>
run
</button>
</td>
<td>
<div id='run'>script results</div>
</td>
</tr>
<tr>
<td>
<a href='run?run=login_cycle_build'>login_cycle_build</a>
</td>
<td>
<button onclick='loadXMLDoc()' type='button'>
run
</button>
</td>
<td>
<div id='run'>script results</div>
</td>
</tr>
推荐答案
jQuery就是用于这种类型的工作的.通常,您不希望将所有样板都放在顶部来创建XHR对象.
jQuery was made for this type of work. Just in general, you don't want to have all that boilerplate on top to create an XHR object.
特别是,我喜欢传递对象文字以使这种通用代码成为可能:
Specifically, I like passing object literals around to make this type of generic code possible:
function generic(params) {
if (params.div && params.div instanceof Array && params.div.size() > 0) {
//multiple divs handling goes here
}
ajax(params.url, params.params);
}
generic({
divs: ['div1','div2'],
url: 'http://google.com',
params: {
param1:'hello',
param2:'hi'
}
})
这篇关于可以使用参数从html调用loadXMLDoc()吗?(ajax)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文