AJAX - Using POST instead of GET

后端 未结 2 1931
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 12:49

Up until now I have been using:

xmlhttp.open(\"GET\",\"server_script.php?q=\"+str,true);

Thanks

Edit: I am providing a solution for an

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-22 13:10

    this is how you would use post:

    var url = "server_script.php";
    var params = "q="+str;
    xmlhttp.open("POST", url, true);
    
    //Send the proper header information along with the request
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    
    xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert(xmlhttp.responseText);
        }
    }
    xmlhttp.send(params);
    

    source

提交回复
热议问题