运行grunt后添加%20(空格)的Javascript URL字符串

Ala*_*lan 1 javascript json d3.js gruntjs

TLDR;

固定如下

selectedValue = selectedValue.replace(/\s+/g, '')
Run Code Online (Sandbox Code Playgroud)

感谢:Richard MacarthyAaron Digulla的回答,这让我想起了正确的答案.

只是很明显,似乎Grunt出于某种原因添加了这个空白.修复非常简单......

原始问题

我有一个JSON请求,它使用d3.js获取要用于数据可视化的JSON文件的内容.

这一切都在本地工作正常,但是当我运行grunt构建时,URL字符串会%20从无处注入其中...

以下是在运行Grunt之前字符串的外观:

d3.json("json/wards-info/"+selectedValue+"-wards-data.json", function(error, newDatas) {
    newData = newDatas;
    newWardsData = newWardsDatas;
    drawMap(newData, newWardsData);
});
Run Code Online (Sandbox Code Playgroud)

其中计算:

http://localhost:8080/app/json/wards-info/liverpool-ward-data.json
Run Code Online (Sandbox Code Playgroud)

运行Grunt后,计算出的URL字符串变为:

http://localhost:8080/dist/json/wards-info/liverpool%20-ward-data.json
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它似乎是%20在两者之间添加liverpool-ward

这是因为咕噜声,还是因为别的什么?

Ric*_*thy 5

%20 通常表示HTML URL编码中的空格,尝试确保输出中没有空格.

你可以使用这样的东西来帮助:

string.replace(/ /g,'')剥去白色空间.其中string是您的URL.

无论是那个还是试试这个:

.replace(/%20/g,'')