Javascript Ajax call using password with special c

2019-08-04 06:08发布

问题:

I have a JavaScript page that makes an Ajax call like the code below. The PHP page is inside a corporate intranet and requires authentication in the domain (basic auth is disallowed). I collect the username (u) and password (p) from input fields using jQuery.

var u = $('#user').val();
var p = $('#pass').val();

$.ajax({
    url: "http://mydomain/mypage.php",
    username: u,
    password: p,
    error: function () {
    }
}).done(function(html) {
        //Do Stuff
});

The solution works very well...except when the user has a special character in their password. So far, it seems we're affected by: @ $ +.

It seems to me there is some conflict collecting passwords with symbols that also act as syntax operators in JavaScript...but how do you properly escape them to be submitted as a password?

Thanks!

回答1:

You will have to URL encode the parameter. For JavaScript, take a look at encodeURIComponent. Backend framework will understand it, so don't worry about that.