ASP.NET 中的 url 解码
我在 asp.net 中有一个带有一些文本框的表单。我正在尝试使用 jquery ajax 将其中的值发布到服务器,但遇到问题。我使用 javascriptencodeURIComponent 作为文本框的值,然后发布,但我看到 url 是自动编码的:
wanted result:
mylefturl/first%20name/last%20name
this is what's actually happenning:
mylefturl/first name/last name
所以我收到一个 asp.net 错误...
我的 javascript 代码:
var firstName = $("#SignupFirstName").val();
var lastName = $("#SignupLastName").val();
var email = $("#SignupEmail").val();
var password = $("#SignupPassword").val();
var url = '/Ajax/GetSignup/' + encodeURIComponent(firstName) + '/' + encodeURIComponent(lastName) + '/' + encodeURIComponent(email) + '/' + encodeURIComponent(password);
$.ajax({
url: u,
...
这个问题的解决方案是什么?
I have a form in asp.net with a few textboxes. I'm trying to post the values in them to the server using jquery ajax, but having problems. I'm using javascript encodeURIComponent for the values of the textboxes, and then posting, but I see that the url is encoded automatically:
wanted result:
mylefturl/first%20name/last%20name
this is what's actually happenning:
mylefturl/first name/last name
and so I get an asp.net error ...
my javascript code:
var firstName = $("#SignupFirstName").val();
var lastName = $("#SignupLastName").val();
var email = $("#SignupEmail").val();
var password = $("#SignupPassword").val();
var url = '/Ajax/GetSignup/' + encodeURIComponent(firstName) + '/' + encodeURIComponent(lastName) + '/' + encodeURIComponent(email) + '/' + encodeURIComponent(password);
$.ajax({
url: u,
...
What is the solution to this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您在发送 AJAX 调用时使用
data
参数:现在 jQuery 将负责对发送到服务器的值进行正确的 url 编码。这些值将作为 POST 有效负载的一部分而不是 url 发送。这是确保它们正确到达服务器的最安全方法。我建议您避免使用用户输入的值作为 url 路径的一部分。看看以下内容问题来了解如果您走那条路可能会遇到什么困难。
I would recommend you to use the
data
parameter when sending the AJAX call:Now jQuery will take care of properly url encoding the values sent to the server. Those values will be sent as part of the POST payload instead of the url. It's the safest way to ensure that they get correctly to the server. I would recommend you to avoid using user entered values as part of your url path. Take a look at the following question to understand what difficulties you might face if you ever go that route.