主要技術(shù):
修改服務(wù)器相應(yīng)的相應(yīng)頭,使其可以相應(yīng)任意域名。and設(shè)置響應(yīng)頭,使其能夠相應(yīng)POST方法。
實(shí)現(xiàn)代碼:
這里先放flask代碼:
html測(cè)試代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <span id="ss">test get</span> <button onclick="getAjax()">click</button> <p id="time">test post</p> <input type="submit" value="click" onclick="getPostAjax()"> <script> function getPostAjax() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function () { if(xmlhttp.readyState=4 && xmlhttp.status ==200 ) { document.getElementById("time").innerText = xmlhttp.responseText; } } xmlhttp.open("POST","http://localhost:5000/test",true); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); #這句話可以發(fā)送post數(shù)據(jù),沒(méi)有此句post的內(nèi)容無(wú)法傳遞 xmlhttp.send(); } function getAjax() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function () { if(xmlhttp.readyState==4 && xmlhttp.status == 200){ document.getElementById("ss").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","http://localhost:5000/test",true); xmlhttp.send(); } </script> </body> </html>
無(wú)法控制響應(yīng)頭
對(duì)于這種情況,get請(qǐng)求可以使用jquery完成,post,無(wú)能為力。
大家學(xué)會(huì)了嗎?趕緊動(dòng)手嘗試一下吧。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com