Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 13th, 2007, 12:59 PM
Registered User
 
Join Date: Apr 2007
Location: , , .
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...

Reply With Quote
  #2 (permalink)  
Old April 13th, 2007, 03:17 PM
jmcpeak's Avatar
Wrox Author
Points: 467, Level: 7
Points: 467, Level: 7 Points: 467, Level: 7 Points: 467, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2005
Location: , Texas, USA.
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
Reply With Quote
  #3 (permalink)  
Old April 15th, 2007, 01:43 PM
Registered User
 
Join Date: Apr 2007
Location: , , .
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("&");
}

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL statement that includes LEFT & RIGHT JOIN in callagga SQL Language 13 June 9th, 2008 03:30 AM
Good way to select & process only one node traxwx XSLT 2 May 21st, 2007 06: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 06:19 AM
sql & join tables & find a field in multiple table trangd Beginning PHP 2 January 29th, 2004 07:18 PM



All times are GMT -4. The time now is 02:42 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.