2010-02-07 75 views
0

我提出,发送textarea的内容到PHP页面获取Ajax请求一个小脚本:发送 n使用jQuery阿贾克斯

$.get('./data/pages/admin/actions.php?savepost=1','&post_id=' + $(box).attr('id') + '&text=' + new_text,'text'); 

NEW_TEXT得到textarea的内容与VAL();

当我从php保存文本时,它没有\ n字符。 Some1可以解决我的问题?谢谢

回答

3

根本没有对数据进行编码,所以很多事情都会爆炸(百分号,&符号...)。无论是使用上的每个位或encodeURIComponent(这是比较容易),让jQuery的为你做它:

$.get('./data/pages/admin/actions.php?savepost=1', { 
    post_id: $(box).attr('id'), 
    text: new_text 
}, 'text'); 

对数据参数的详细信息,请参阅the docs

+0

感谢您的帮助:d – netcelli 2010-02-08 08:49:56

+0

@netcelli:很荣幸,很高兴它帮助。 – 2010-02-08 08:55:30

2

使用encodeURIComponent()将换行符编码为正确的URL符号。

-2

通过之前new_text请先尝试这样做。

new_text.replace("\r\n","%0D%0A"); 
+0

“\ n”只是“%0A”。 “\ r \ n”(Windows换行符)将是“%0D%0A”! – AndiDog 2010-02-07 16:20:15

+0

是的......现在已经太迟了,我没有意识到......当我尝试编辑SO页面时说维护中......叹息...... – Reigel 2010-02-07 16:30:44

+0

无论如何,它只解决了问题的一个非常狭窄的部分。 – 2010-02-07 16:33:01