tags:

views:

24

answers:

1

Hi all,

I was doing some self-learning on how to pass data from JQuery Ajax to a particular URL in CakePHP:
I have tested three sets of codes that the first one was working well, but the rest failed to work, which makes me so confused.
Could some experts here tell why the second and the third sets of codes failed to pass any data?

Set 1:

<input type=text name="data[User][name]" id="data[User][name]" size="36" maxlength="36"/>
var usr = $("#data\\[User\\]\\[name\\]").val();
$.post(
"http://www.washington.byethost18.com/site1/toavail/"+usr,
function(msg){alert(msg);}
);

Set 2:

<input type=text name="data[User][name]" id="data[User][name]" size="36" maxlength="36"/>
var usr = $("#data\\[User\\]\\[name\\]").val();
$.post(
"http://www.washington.byethost18.com/site1/toavail/",
 {queryString: ""+usr+""},
 function(msg){alert(msg);}
 );

Set 3:

<input type=text name="data[User][name]" id="data[User][name]" size="36" maxlength="36"/>
var usr = $("#data\\[User\\]\\[name\\]").val();
$.post(
"http://www.washington.byethost18.com/site1/toavail/",
 usr,
 function(msg){alert(msg);}
);
+1  A: 

Because you wrote, that only the first version work, your server wait for the URL like

http://www.washington.byethost18.com/site1/toavail/paramVal

and not like

http://www.washington.byethost18.com/site1/toavail/

with "queryString=paramVal" or "paramVal" as posted data body. All three version send with the value usr variable in different way.

A small remark: I recommend to use encodeURIComponent function to encode data which your send as a part of url.

Oleg