如何使用javascript作为事件处理程序发送带有post/get方法的http请求?谢谢!保罗在事件处理程序中发送没有XHR的http请求
3
A
回答
4
好吧,你不想使用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();
}
1
可以使用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);
而且不要忘记以编码PARAM使用encodeURIComponent
参数在用户输入的情况下的值编码
例如
params="paramName="+encodeURIComponent(paramValue);
1
示例代码:
var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "test.xml");
client.send();
function handler()
{
// your handler
}
0
阿贾克斯教程(http://code.google.com/edu/ajax/tutorials/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");
}
}
}
1
这样做的标准类是XmlHttpRequest
,但它没有得到普遍支持。在某些浏览器中,您必须改用ActiveXObject("Microsoft.XMLHTTP")
。
查看jQuery系统,该系统提供HTTP下载(AJAX样式)方法,无论基础浏览器API如何(因此避免了Tzury答案中显示的很多代码)。
jQuery的AJAX文档可以http://docs.jquery.com/Ajax
1
你应该尝试添加在一个隐藏字段atring,然后调用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>
相关问题
- 1. 有没有办法转发Http处理程序中的请求
- 2. Ajax发送2个POST请求...我没有任何live()事件处理程序
- 3. 没有HTTP处理程序在DotNetNuke的发现请求类型“GET”
- 4. 在Golang中处理并发HTTP请求
- 5. 使用XHR请求发送文件
- 6. 通过Cookie发送Chrome扩展程序中的XHR请求
- 7. jQuery事件处理程序有时在IE中没有触发
- 8. Android Http请求处理程序
- 9. NodeJS http请求处理程序
- 10. WP7 Http请求处理程序
- 11. 发送和处理.json请求(在Rails应用程序中)
- 12. iOS - 在异常处理程序中发送Web请求失败
- 13. 有没有办法在控制台应用程序中处理http post请求?
- 14. 如何在BroadcastReceiver事件上发送HTTP POST请求OnReceive事件
- 15. 如何处理在Node中发送多个http请求?
- 16. HTTP请求流在请求处理程序外不可读
- 17. 事件处理程序没有在WebForms中触发
- 18. 在Python中处理XHR请求的有效方式Flask
- 19. 处理HTTP请求
- 20. 发送HTTP请求
- 21. 发送HTTP请求
- 22. jquery ajax post()请求没有发送XHR头
- 23. $ http请求没有被请求拦截器处理
- 24. 如何在c#中处理没有http头的SOAP/XML请求?
- 25. 子对象中的AS3事件处理程序没有触发
- 26. XML HTTP请求在事件处理函数中不起作用
- 27. 如何在akka.net的事件处理程序中发送消息?
- 28. 在iPHone应用程序中发送http请求的框架
- 29. 使用SQL Server代理程序发送HTTP请求作业
- 30. 多线程事件处理请求
这就是所谓的Ajax和谷歌会比我们更快的帮助你;) – 2009-10-25 08:04:35
对不起,我没有做出明确的问题:我试着不使用XMLHttpRequest来办。有没有其他方法? – Paul 2009-10-25 08:25:33
您可以提交表单以发送获取或发布。我添加了一个关于如何做到这一点的答案。 – JAL 2009-10-25 09:21:41