admin管理员组文章数量:1026989
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
3 Answers
Reset to default 4encodeURIComponent
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
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
3 Answers
Reset to default 4encodeURIComponent
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
本文标签: javascriptEscaping a forward slashfor variable in Ajax URLStack Overflow
版权声明:本文标题:javascript - Escaping a forward slashfor variable in Ajax URL - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745528052a2154619.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论