我已阅读论坛和谷歌,但我只是不明白如何将我的ajax GET查询更改为POST。如果有人能帮助我实现它会很棒。谢谢!
继承我的代码:
function ajax(query,parameters,progress_div,progress_txt,result_div) {
// Sisend:
// 0 or 1 | (main_error) error string OR (resdiv) result string
var xmlhttp;
if (progress_div) { progdiv = document.getElementById(progress_div); }
if (result_div) { resdiv = document.getElementById(result_div); }
if (progdiv) { progdiv.innerHTML = progress_txt; }
// ajax
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = xmlhttp.responseText;
var string = response.split("<?php echo $vs; ?>");
//kui päring oli ok
if (string[0] == '1' || string[0] == '0') {
if (progdiv) { progdiv.innerHTML = ''; }
if (resdiv) { resdiv.innerHTML = string[2]; }
}
else {
errdiv = document.getElementById('main_error');
if (string[0] == '0') { errdiv.innerHTML = string[2]; }
else { errdiv.innerHTML = string[0]+string[1]; }
progdiv.innerHTML = '';
errdiv.style.display = 'block';
}
if (string[0] == '1' && string[1] != '0') {
window.location.href = string[1];
}
}
}
xmlhttp.open('GET','?leht=ajax&query='+query+'¶meters='+parameters,true);
xmlhttp.send();
return false;
}
答案 0 :(得分:-1)
更改以下行
xmlhttp.open('GET','?leht=ajax&query='+query+'¶meters='+parameters,true);
像这样
var url = 'http://yoursite.com/yourfile.php?leht=ajax&query='+query+'¶meters='+parameters;
xmlhttp.open("GET", url, true); //GET method
xmlhttp.open("POST", url, true); //POST method
您错过了行中的文件名。
Jquery POST
$.ajax({ url: "yourfilename.php",
data: {leht: 'ajax',"query":query,"parameters":parameters},
type: 'post',
success: function(output) {
//process the output
}
});