O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sending a POST Request from JavaScript

var xmlhttp = new XMLHttpRequest(); var params = "first=Brad&last=Dayley&email=brad@dayleycreations.com"; xmlhttp.setRequestHeader("Content-type",   "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", params.length); xmlhttp.onreadystatechange=function() {   if (xmlhttp.readyState==4 && xmlhttp.status==200)     { alert("Email Updated: " + xmlhttp.responseText); } } xmlhttp.open("POST","setUserEmail.php",true); xmlhttp.send(params);

To send a POST request from JavaScript, you first need to implement an onreadystatechange event handler to handle the response or responseText values. For example:

xmlhttp.onreadystatechange=function() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required