我想使用 AJAX 创建一个函数,但是当我使用 POST 方法发送信息时,该函数不起作用,但 GET 起作用方法有效
这是我的function :
function ajax(page,method,id,send,a){
a = a || true;
page = page || '';
method = method || 'GET';
id = id || '';
send = send || '';
if(page!='' || id!=''){
var xmlhttp;
if(window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
if(!xmlhttp)
xmlhttp = new ActiveXObject("Mmxl2.XMLHTTP.3.0");
if(!xmlhttp)
xmlhttp = new ActiveXObject("Mmxl2.XMLHTTP.6.0");
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readystate == 4 || xmlhttp.status == 200)
document.getElementById(id).innerHTML = xmlhttp.responseText;
};
if(send!=''){
if(method=='get' || method=='GET'){
xmlhttp.open("GET",page+'?'+send,a);
xmlhttp.send();
}
else if(method=='post' || method=='POST'){
xmlhttp.open('POST',page,a);
xmlhttp.SetRequestHeader('Conect-Type','application/x-www-form-urlencoded');
xhttp.send(send);
}
}
}
}
最佳答案
请在您的问题中粘贴您的 fiddle 内容,因为链接可能会消失。 您的代码中对我来说有什么问题是这一行:
xmlhttp.SetRequestHeader('Conect-Type','application/x-www-form-urlencoded');
'Conect-Type'
应替换为 'Content-Type'
。
关于javascript - 在ajax中使用post方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45935116/