题
如何使用javascript作为事件处理程序使用post / get方法发送http请求?谢谢!保罗
解决方案
好的,你不想使用Ajax。 您可以使用事件处理程序提交表单!
<a href='#' onclick='cow_submit("zoodle")'>send</a>
<form method='post' id='formie' action='find_some_action.php'>
<input type='hidden' id='snoutvar' name='snoutvar' value='snout'>
</form>
<script>
function cow_submit(a_var_to_set){
var plip=document.getElementById('formie');
var snout=document.getElementById('snoutvar');
snout.value=a_var_to_set;
plip.submit();
}
其他提示
示例代码:
var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "test.xml");
client.send();
function handler()
{
// your handler
}
您可以使用 XMLHttpRequest 从javascript发送请求
发送GET请求
var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("GET", url+"?"+params, true);
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(null);
发送POST请求
var url = "get_data.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
在用户输入的情况下,不要忘记使用 encodeURIComponent
对参数进行编码以进行参数值编码
e.g。
params="paramName="+encodeURIComponent(paramValue);
执行此操作的标准类是 XmlHttpRequest
,但它并非普遍支持。在某些浏览器上,您必须使用 ActiveXObject(&quot; Microsoft.XMLHTTP&quot;)
。
查看 jQuery 系统,该系统提供HTTP下载(AJAX样式)方法,无论底层浏览器API如何(因此避免在Tzury的回答中显示很多代码。)
jQuery AJAX文档位于 http://docs.jquery.com/Ajax
您应该尝试在隐藏字段中添加字符串,然后调用form.submit()将表单提交到操作中的页面定义。
<script type="text/javascript">
function doTestFormSubmit(yourString) {
document.getElementById("myString").value=myString;
document.getElementById("testForm").submit();
}
</script>
<form name="testForm" id="testForm" action="yourDesiredPage.php" method="post">
<input type="hidden" name="myString" id="myString" value=""/>
</form>
Ajax教程( http://code.google.com/ EDU / AJAX /教程/ AJAX-tutorial.html )
var obj;
function ProcessXML(url) {
// native object
if (window.XMLHttpRequest) {
// obtain new object
obj = new XMLHttpRequest();
// set the callback function
obj.onreadystatechange = processChange;
// we will do a GET with the url; "true" for asynch
obj.open("GET", url, true);
// null for GET with native object
obj.send(null);
// IE/Windows ActiveX object
} else if (window.ActiveXObject) {
obj = new ActiveXObject("Microsoft.XMLHTTP");
if (obj) {
obj.onreadystatechange = processChange;
obj.open("GET", url, true);
// don't send null for ActiveX
obj.send();
}
} else {
alert("Your browser does not support AJAX");
}
}
function processChange() {
// 4 means the response has been returned and ready to be processed
if (obj.readyState == 4) {
// 200 means "OK"
if (obj.status == 200) {
// process whatever has been sent back here:
// anything else means a problem
} else {
alert("There was a problem in the returned data:\n");
}
}
}
不隶属于 StackOverflow