不使用jQuery的Ajax
不使用jQuery的Ajax
这个问题已经有了答案:
可能是重复的:
我有一份js(Ajax)代码,但我想让它不使用Ajax(XMLHttpRequest)
$.ajax({ type:'POST', data:'slug='+prize, url:'/wybrana-nagroda/', success:function(msg) { $('#whaiting').hide(); if(msg==='winner') $(window.location).attr('href','/formularz'); } });
应该长什么样子?
function send(post, url) { var client = new XMLHttpRequest(); client.open("POST", url); client.send(message); }
?
谢谢。
admin 更改状态以发布 2023年5月21日
如果你想让它在所有浏览器上兼容,你需要做如下操作:
function sendRequest(url,callback,postData) { var req = createXMLHTTPObject(); if (!req) return; var method = (postData) ? "POST" : "GET"; req.open(method,url,true); req.setRequestHeader('User-Agent','XMLHTTP/1.0'); if (postData) req.setRequestHeader('Content-type','application/x-www-form-urlencoded'); req.onreadystatechange = function () { if (req.readyState != 4) return; if (req.status != 200 && req.status != 304) { // alert('HTTP error ' + req.status); return; } callback(req); } if (req.readyState == 4) return; req.send(postData); } var XMLHttpFactories = [ function () {return new XMLHttpRequest()}, function () {return new ActiveXObject("Msxml2.XMLHTTP")}, function () {return new ActiveXObject("Msxml3.XMLHTTP")}, function () {return new ActiveXObject("Microsoft.XMLHTTP")} ]; function createXMLHTTPObject() { var xmlhttp = false; for (var i=0;i
致谢:QuirksMode