Wrox Programmer Forums
|
BOOK: Professional Ajax ISBN: 978-0-471-77778-6
This is the forum to discuss the Wrox book Professional Ajax by Nicholas C. Zakas, Jeremy McPeak, Joe Fawcett; ISBN: 9780471777786
Important: For the new 2nd edition of this book, please post here instead: [url="http://p2p.wrox.com/forum.asp?FORUM_ID=307"]http://p2p.wrox.com/forum.asp?FORUM_ID=307[/url]
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Ajax ISBN: 978-0-471-77778-6 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 April 13th, 2007, 11:59 AM
Registered User
 
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default "&" is not a good join char!

Hi,
On chapter 2, in the first box code you wrote:

return aParams.join("&");

I think "&" is not a good char to make join()... When there's a "&" in a element value you will have an incoplete post!

How can I fix this?

Thanks...

 
Old April 13th, 2007, 02:17 PM
jmcpeak's Avatar
Wrox Author
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
Default

Howdy, erme2.

Joining the elements of the array with "&" is a requirement for HTTP communication. The string returned by aParams.join() is the request body sent to the server, and "&" has to separate the key/value pairs. No other character can be used.

You do have a point that problems can arise if "&" is in an element of the array. However, the code in Chapter 2 makes this a non-issue by encoding both the key and the value with the encodeURIComponent() method. Here is the getRequestBody() function as detailed in Chapter 2:

Code:
function getRequestBody(oForm) {
    var aParams = new Array();

    for (var i=0 ; i < oForm.elements.length; i++) {
        var sParam = encodeURIComponent(oForm.elements[i].name);
        sParam += "=";
        sParam += encodeURIComponent(oForm.elements[i].value);
        aParams.push(sParam);
    } 

    return aParams.join("&");
}
Any "&" that is passed to encodeURIComponent() ends up as %26, effectively dodging any issue that would otherwise arise with "&".

------------------------
Jeremy McPeak
Author of:
Professional Ajax, 2nd Edition
Beginning JavaScript, 3rd Edition

http://www.wdonline.com
 
Old April 15th, 2007, 12:43 PM
Registered User
 
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"Any "&" that is passed to encodeURIComponent() ends up as %26, effectively dodging any issue that would otherwise arise with "&"."

You are right, I was so tired...
Really my problems started whit checkbox and radio elements, I solved with a switch case but writing the default case I omitted some code...

Thanks for your help I solved so...
(it works!)

function getRequestBody(funct_form) {
    var aParams = new Array();
    for (i=0; i<funct_form.elements.length; i++) {
        switch (funct_form.elements[i].type) {
            case 'radio':
                if (funct_form.elements[i].value=='S' && funct_form.elements[i].checked) { var sParam = funct_form.elements[i].name+"=S"; aParams.push(sParam); }
                else { var sParam = funct_form.elements[i].name+"=N"; aParams.push(sParam); }
                break;
            case 'checkbox':
                if (funct_form.elements[i].checked==true) { var sParam = funct_form.elements[i].name+"="+funct_form.elements[i].value; aParams.push(sParam); }
                break;
            default:
                var sParam = encodeURIComponent(funct_form.elements[i].name);
                sParam += "=";
                sParam += encodeURIComponent(funct_form.elements[i].value);
                aParams.push(sParam);
        }
    }
    return aParams.join("&");
}






Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL statement that includes LEFT & RIGHT JOIN in callagga SQL Language 13 June 9th, 2008 02:30 AM
Good way to select & process only one node traxwx XSLT 2 May 21st, 2007 05:08 PM
HELP:Join & Split Projects/soln Aritra ASP.NET 1.0 and 1.1 Basics 4 March 2nd, 2007 03:04 AM
UPDATE & JOIN (and WHERE) pete_m SQL Server 2000 2 September 28th, 2004 05:19 AM
sql & join tables & find a field in multiple table trangd Beginning PHP 2 January 29th, 2004 07:18 PM





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