Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在javascript中获取具有特殊字符URL的内容?_Javascript_Jquery - Fatal编程技术网

无法在javascript中获取具有特殊字符URL的内容?

无法在javascript中获取具有特殊字符URL的内容?,javascript,jquery,Javascript,Jquery,我正在使用javascript从url获取html内容。我将遵循下面的代码: function getContentHTML(theUrl) { $.ajax({ url: theUrl, crossDomain: true, success: function (data) { $("#divcontent").html(data); } }); } 如果URL是英文的(例如:bbc.co

我正在使用javascript从url获取html内容。我将遵循下面的代码:

function getContentHTML(theUrl) {
    $.ajax({
        url: theUrl,
        crossDomain: true,
        success: function (data) {
            $("#divcontent").html(data);
        }
    });
}
如果URL是英文的(例如:bbc.com,voa.com…),这是可以的,但如果URL包含特殊字符,我无法获取内容。我如何解决?你可以在我的URL中测试


URL:

尝试此功能解码URL:

  var uri_dec = decodeURIComponent(encodedUrl);


这是因为
url
已编码,浏览器会自动将一些字符放在url中的特殊字符的位置,例如在url中插入
空格的位置%20

因此,您必须对url进行编码解码,在以下链接中解释得非常清楚:


url中不允许使用特殊字符。您需要对特殊字符进行编码。使用javascript中可用的encodeURI函数。查看rfc 1738了解更多详细信息

 var uri_dec = decodeURI(encodedUrl);
var encodeduri=https://tw.news.yahoo.com/%E7%A9%BA%E9%9B%A3%E8%BB%8D%E6%96%B9%E5%A4%A7%E6%94%B9%E5%8F%A3-%E7%A2%BA%E5%AF%A6%E6%9C%89%E4%BB%8B%E5%85%A5%E8%88%AA%E7%AE%A1-120050818.html

 var uri_decoded = decodeURIComponent(encodeduri);  //The decodeURIComponent() function decodes a URI component.

 OR

 var uri_decoded = decodeURI(encodeduri);  //The decodeURI() function is used to decode a URI.