我在这里得到了这段工作代码:
$(document).ready(function(){
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
$('#response').hide();
$('#addroom').click(function(){
var url = "./scripts/addnew_room.php";
var room_num = document.getElementById('rm_num').value;
var room_type = document.getElementById('rm_type').value;
var tosend = "&rm_num="+room_num+"&rm_type="+room_type;
ajaxRequest.open("POST", url, true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
var return_data = ajaxRequest.responseText;
document.getElementById("response").innerHTML = return_data;
}
}
ajaxRequest.send(tosend);
$('#response').show();
document.getElementById("response").innerHTML = '<img src="./ajax-images/ajax-loader.gif">';
});
});
有人能告诉我为什么它没有将变量发送到 POST 方法,即使我设置了 POST 方法,它仍然使用 GET 方法并扰乱了我的页面!!!
最佳答案
你可以这样使用
$.POST("url.com", {name: "John", location: "Boston"}, function(){
})
POST()
是一个jquery函数
你也可以使用 jquery ajax()
功能
$.ajax({
type:"POST",
url:"url.php",
data: { name: "John", location: "Boston" },
}).done(function(data){
// this is very simple to get or post data
$("#response").html(data);
})
关于php - 如何使 AJAX 使用 POST 而不是 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12775427/