Doing a form POST in Spry

I blogged about half a year ago on how to send data with Spry. One thing I had not done (until recently) was send a form POST with Spry. Keith, over on the Spry forums, posted a good example on how to do it. With his permission I’m reposting his example. (With a slight modification or two.) The basic syntax to do a POST with Spry works like so:

Spry.Utils.loadURL('POST', url, true, resFunc, {postData: formData, headers: {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}});

The first argument simply specifies that we are using a POST instead of a GET operation. The second argument is the URL to load. The third argument specifies if the operation should be asynchronous or not. The next argument is the function to run when the HTTP operation is finished. The next set of arguments are options to pass along to the HTTP operation. The headers should be used as is, and formData in this example points to a string that contains your form information. Here is an example:

var fname = $("FName").value; var lname = $("LName").value; var cred = $("Credentials").value;

//Create a string of data var formData = ‘firstname=’+fname+’&lastname=’+lname+’&credentials=’+cred; formData = encodeURI(formData); </code>

By the way - I plan on writing a simpler method to handle this.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for Extend by Auth0. He focuses on serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments