Reputation: 547
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 ?
Upvotes: 0
Views: 894
Reputation: 1038850
I would recommend you to use the data
parameter when sending the AJAX call:
var firstName = $("#SignupFirstName").val(),
lastName = $("#SignupLastName").val(),
email = $("#SignupEmail").val(),
password = $("#SignupPassword").val();
$.ajax({
url: '/Ajax/GetSignup/',
type: 'POST',
data: {
firstName: firstName,
lastName: lastName,
email: email,
password: password
},
success: function(result) {
// TODO: process the results from 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.
Upvotes: 3