我有点忘了PHP,有没有更简单的方法使用JavaScript AJAX发布表单,不想简单地添加jQuery来发布ajax请求,而不必传递参数?
我想通过Ajax发布表单而不必获取参数并在调用中发送它们,这可能吗?是否有替代以下代码...
var mypostrequest=new ajaxRequest()
mypostrequest.onreadystatechange=function(){
if (mypostrequest.readyState==4){
if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
document.getElementById("result").innerHTML=mypostrequest.responseText
}
else{
alert("An error has occured making the request")
}
}
}
var namevalue=encodeURIComponent(document.getElementById("name").value)
var agevalue=encodeURIComponent(document.getElementById("age").value)
var parameters="name="+namevalue+"&age="+agevalue
mypostrequest.open("POST", "basicform.php", true)
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
**mypostrequest.send(parameters)**
我打算使用POST而不是GET隐藏URL上发送的内容,这感觉很奇怪,而且与使用GET相同。或者我读错了吗?
答案 0 :(得分:-3)
如果你只想要一些简单的简单Ajax,就不要使用jQuery。
这样就可以了。
// Vanilla
var httpRequest = new XMLHttpRequest()
httpRequest.onreadystatechange = function (data) {
// code
}
httpRequest.open('GET', url)
httpRequest.send()
现在我们还可以添加更多的浏览器支持(IE6和更早版本:http://caniuse.com/#search=XMLHttpRequest)@所有jQuery头:jQuery 2放弃了对IE8的支持以及更早的支持,因此没有'额外支持'。
// creates an XMLHttpRequest instance
function createXMLHttpRequestObject()
{
// xmlHttp will store the reference to the XMLHttpRequest object
var xmlHttp;
// try to instantiate the native XMLHttpRequest object
try
{
// create an XMLHttpRequest object
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
// assume IE6 or older
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
catch(e) { }
}
// return the created object or display an error message
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
所有荣誉都转到:http://www.cristiandarie.ro/asp-ajax/Async.html
这篇文章是由谷歌赞助的(一个非常强大的工具,你输入的东西,它提供更多的东西与答案)