Wrox Programmer Forums
|
BOOK: Professional JavaScript for Web Developers 3rd edition
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 3rd Edition by Nicholas C Zakas; ISBN: 978-1-1180-2669-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 3rd edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2012, 08:07 PM
Registered User
 
Join Date: May 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XMLHttpRequest and POST question

Hi,

I am looking at using XMLHttpRequest with a POST request. The example on page 709 works for a form, but what if I have what I want to POST in a JavaScript string variable already?

Essentially I am looking for an elegant way to transfer a string from browser JavaScript to a LAMP server. I have done it with code that creates a form and then posts the form but that seems rather a crude approach.

(Enjoying the book by the way.)
 
Old May 10th, 2012, 04:10 PM
Registered User
 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
Default

AFAIK all you need to do is construct a query string using names & values, pass it to send(), i.e.
Code:
xhr.send("arg1=value1&arg2=value2&arg3=value3");
that way, you're simulating how the form variables are passed to the server.
e.g.
Code:
xhr.send("user=john&[email protected]");
where 'user' and 'email' take the place of form field names, and 'john' and '[email protected]' their respective values.

In general, to be safe, you need to call encodeURIComponent() for each arg name and value, as in the serialize() function on pages 540 - 541

HTH

Alan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on HTTP Post dgrumbli BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 3 December 20th, 2006 02:03 PM
Old post from a year ago, had question plz galacticsoul General .NET 0 August 4th, 2006 12:01 PM
vbscript question (not sure where to post this) interrupt Javascript How-To 0 February 14th, 2006 10:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.