Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript和Java中的参数_Java_Javascript_Ajax - Fatal编程技术网

Javascript和Java中的参数

Javascript和Java中的参数,java,javascript,ajax,Java,Javascript,Ajax,我目前正在向一个servlet发送一个ajax请求,并且在发送过程中丢失了一些信息。我关心的参数(丢失数据)是“comment”参数。下面你可以看到我的最后4行ajax var params = "name=" + name + "&email=" + email + "&comment=" + comment + "&player_id=" + player_id; xmlhttp.open("POST", 'comment', true); xmlhttp.s

我目前正在向一个servlet发送一个ajax请求,并且在发送过程中丢失了一些信息。我关心的参数(丢失数据)是“comment”参数。下面你可以看到我的最后4行ajax

var params = "name=" + name + "&email=" + email + "&comment=" + comment + "&player_id=" + player_id;    
xmlhttp.open("POST", 'comment', true);
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.send(params);
当我在发送之前和声明之后提醒我的参数时,它们看起来如下所示:

name=Chris&email=email@gmail.com&comment=Hey, check this song out on groovershark http://grooveshark.com/#!/s/Here+feat+Soulive/2YDJIw?src=5&player_id=4
encodeURIComponent(params);
xmlhttp.send(params);
但是,在我的servlet中,如果我在获得comment参数后立即为其打印行,则会得到以下输出:

Hey, check this song out on groovershark http://grooveshark.com/#!/s/Here feat Soulive/2YDJIw?src=5

问题是,
“+”
标记在从ajax到容器的请求中消失了,我不知道为什么。我已经把它缩小到这个问题区域了。最后,b/c我实际上没有注意到这一点,直到在另一个堆栈溢出成员的帮助下,在数据库连接中。因此,如果有人能让我知道我能做些什么来找回我的
“+”
字符,我将非常感激!非常感谢你

您应该对URI的参数部分进行百分比编码:

这样,+就变成了%2B,可以在服务器端正确解释


查看encodeURIComponent()函数:

您应该在发送参数之前对其进行编码,如下所示:

name=Chris&email=email@gmail.com&comment=Hey, check this song out on groovershark http://grooveshark.com/#!/s/Here+feat+Soulive/2YDJIw?src=5&player_id=4
encodeURIComponent(params);
xmlhttp.send(params);

+
正在转换为空格,您需要对url进行编码。很抱歉,我不知道如何对参数进行编码。我尝试了编码组件(params);但这并没有改变任何事情。在服务器端仍有空间不是%2B。很抱歉,我该怎么办呢?非常感谢。如果你有时间,我可以和你谈两分钟吗?在你编码参数后,我有一个关于@Nelson的快速问题。您需要在servlet中使用URLDecode进行解码