简体   繁体   中英

prevent special character in ajax (http)

 $.ajax({
        url: '/pos/' + myVar
                type: 'GET',
                success: function(data) {}
..

myVar's value is ab but it's not consistent, it may be a->b, how could I prevent it to become -%3E?

If the result URL becomes /pos/myVar you can use '/pos/'+encodeURIComponent(myVar) . ie,

$.ajax({
        url: '/pos/' + encodeURIComponent(myVar),
        type: 'GET',
        success: function(data) {}
});

Else if your result URL becomes /pos/myVar_name=myVar_value , then you should try the following :-

$.ajax({
        url: '/pos/',
        data:{myVar_name:"myVar_value"}, /*---*/
        type: 'GET',
        success: function(data) {}
});

This method will automatically encode the required data in the name and value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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