通过PHP的urlencode
使用urlencode 可以实现接口访问中参数值为URL网址的情况。
解决了字符串中由于敏感字符无法传参的问题。
描述:
- urlencode ( string $str ) : string
This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.
urlencode()编码:对字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)。
urldecode()解码:还原 URL 编码字符串。
示例:
<?php
echo urlencode("https://dfq.talk.com/test.html?user=blackbox&room=5ca481e99a88ab02be37bdf3&console=true");
?>
//输出结果如下。
//https%3A%2F%2Fdfq.talk.com%3A3004%2Ftest.html%3Fuser%3Dblackbox%26room%3D5ca481e99a88ab02be37bdf3%26console%3Dtrue
//如果想查看原网址,使用urldecode()解码即可。
参考链接:
https://blog.csdn.net/resilient/article/details/83177100
通过JavaScript的encodeURI()、encodeURIComponent()函数
示例
$scope.send_email_invite=function(){
var inviteUrl=encodeURIComponent(location.href);
var email=$(".email_value").val();
const Http = new XMLHttpRequest();
var send_email_url = "https://dfq.test.com/send_email.php?name="+$scope.localUserName+"&email="+email+"&url="+inviteUrl;
Http.open("GET",send_email_url);
Http.send();
Http.onreadystatechange = function ( ) {
if (this.readyState == 4 && this.status == 200) {
var HttpResponseObj = JSON.parse(Http.responseText);
console.log(HttpResponseObj);
}
}
}
参考链接:
https://www.w3school.com.cn/jsref/jsref_encodeuri.asp
https://www.cnblogs.com/seasons1987/p/3357775.html