开发者

Is this the only way to pass a parameter for Cakephp to work with JQuery Ajax

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开发者_如何学C\\[User\\]\\[name\\]").val();
$.post(
"http://www.washington.byethost18.com/site1/toavail/",
 usr,
 function(msg){alert(msg);}
);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜