最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Escaping a forward slashfor variable in Ajax URL - Stack Overflow

programmeradmin0浏览0评论

I'm working with existing code which validates a user's submission by sending to a given URL via Ajax:

var url = '/foo/bar/' + valuea + '/' + valueb + '/' + valuec; 

Which is then sent via ajax:

 $.ajax({
    type : 'GET',
    url  : url,
    // data : data,
    success :  function(response)
    {...
    }
 });

The issue lies when the user input contains a forward slash e.g.:

valuec = ABC/DEF

As then the url bees defined as:

/far/bar/{valuea}/{valueb}/ABC/DEF

Which results in a whole different URL structure and 404s.

Is there a way I can escape this value to maintain the required URL structure?

I've tried the likes of replace(/\//, '\/'); and also encodeURIComponent() but neither help.

My current thinking is a workaround to rewrite any '/' in the user submitted data to something else (e.g. |) so I can parse it to the URL, and then re-adjust it upon return, but that's a bit of a messy workaround imho.

Thanks in advance

I'm working with existing code which validates a user's submission by sending to a given URL via Ajax:

var url = '/foo/bar/' + valuea + '/' + valueb + '/' + valuec; 

Which is then sent via ajax:

 $.ajax({
    type : 'GET',
    url  : url,
    // data : data,
    success :  function(response)
    {...
    }
 });

The issue lies when the user input contains a forward slash e.g.:

valuec = ABC/DEF

As then the url bees defined as:

/far/bar/{valuea}/{valueb}/ABC/DEF

Which results in a whole different URL structure and 404s.

Is there a way I can escape this value to maintain the required URL structure?

I've tried the likes of replace(/\//, '\/'); and also encodeURIComponent() but neither help.

My current thinking is a workaround to rewrite any '/' in the user submitted data to something else (e.g. |) so I can parse it to the URL, and then re-adjust it upon return, but that's a bit of a messy workaround imho.

Thanks in advance

Share Improve this question edited Oct 25, 2017 at 3:15 artgb 3,2536 gold badges20 silver badges40 bronze badges asked Oct 25, 2017 at 2:25 rjbathgaterjbathgate 3194 silver badges17 bronze badges 1
  • Have you tried stackoverflow./questions/36469397/… or replace('/', '/')? – Nick Cordova Commented Oct 25, 2017 at 2:33
Add a ment  | 

3 Answers 3

Reset to default 4

encodeURIComponent will work.FIDDLE

valuea = 1;
valueb = 2;
valuec = "ABC/DEF";
var url = '/foo/bar/' + valuea + '/' + valueb + '/' + encodeURIComponent(valuec); 
document.write(url+'<br/>');

You can pass the values as query string parameters

var url = "/foo/bar/?valuea=" + valuea + "&valueb=" + valueb + "&valuec=" + valuec; 

Thanks for replies. Unfortunately due to prebuilt limitations I cannot amend the URL structure, and the encodeURIComponent doesn't work in:

    $.ajax({
                type : 'GET',
                url  : url,

So, instead I've gone for the replace method...!

    valuec = valuec.replace(/\//g, '');   

So it parses to the API correctly, and then at that end, I swap them back.

Cheers

发布评论

评论列表(0)

  1. 暂无评论