简体   繁体   English

Javascript和Java中的参数

[英]Parameters in Javascript and Java

I'm currently sending an ajax request to a servlet and losing some information along the way. 我目前正在向Servlet发送ajax请求,并且在此过程中丢失了一些信息。 The parameter I am concerned about(losing data from) is the "comment" parameter. 我关注的参数(丢失数据)是“评论”参数。 Below you can see my last 4 lines of ajax. 在下面,您可以看到我的最后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);

When I alert my params before the send and after the declaration they look like this: 当我在发送之前和声明之后提醒我的参数时,它们如下所示:

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

However in my servlet if I do a print line for the comment parameter right after getting it I get this output: 但是,在我的servlet中,如果我在获取注释参数后立即在它的打印行中得到以下输出:

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

The problem is the "+" marks disappear somewhere along the request from ajax to the container and I have no idea why. 问题是从ajax到容器的请求中, "+"标记消失了,我不知道为什么。 I have narrowed it down to this problem area finally b/c I actually did not notice this until all the way in the database connection with the help of another stack overflow member. 我最终将其范围缩小到了这个问题区域b / c实际上,直到在另一个堆栈溢出成员的帮助下,在数据库连接中一直没有注意到这一点。 So if anyone could let me know what I can do to get my "+" characters back I would really really appreciate! 因此,如果有人能让我知道我该怎么做才能找回我的"+"字符,我将非常感谢! Thank you so much! 非常感谢!

You should encode your params prior to send them, like this: 您应该在发送参数之前对其进行编码,如下所示:

encodeURIComponent(params);
xmlhttp.send(params);

You should percent encode the params part of the URI: http://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters 您应该对URI的params部分进行百分比编码: http : //en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters

In this way + becomes %2B and can be interpreted correctly on the server side. 通过这种方式,+变为%2B,并且可以在服务器端正确解释。

Check out the encodeURIComponent() function: http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp 检出encodeURIComponent()函数: http : //www.w3schools.com/jsref/jsref_encodeuricomponent.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM