POST Request (Javascript)

前端 未结 3 890
野趣味
野趣味 2020-12-06 00:33

How do you make a simple POST request in Javascript without using a forms and without posting back?

3条回答
  •  日久生厌
    2020-12-06 00:54

    Though I am taking the code sample from @sundeep answer, but posting the code here for completeness

    var url = "sample-url.php";
    var params = "lorem=ipsum&name=alpha";
    var xhr = new XMLHttpRequest();
    xhr.open("POST", url, true);
    
    //Send the proper header information along with the request
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    xhr.send(params);
    

提交回复
热议问题