admin管理员组

文章数量:1335600

TLDR;

FIXED AS FOLLOWS

selectedValue = selectedValue.replace(/\s+/g, '')

Thanks to: Richard Macarthy and Aaron Digulla for the answer, which led me down the poath to the correct answer.

Just tp be clear, it seems Grunt was adding this whitespace for some reason. The fix is very simple...

ORIGINAL QUESTION

I have an JSON request, which get the contents of a JSON file to be used for data visualisations using d3.js.

This all works fine locally, but when I run grunt build the URL string gets an %20 injected into it from nowhere...

Here is how the string looks before I run Grunt:

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

Which putes to:

http://localhost:8080/app/json/wards-info/liverpool-ward-data.json

After I run Grunt build the puted URL string changes to:

http://localhost:8080/dist/json/wards-info/liverpool%20-ward-data.json

As you can see, it appears to be adding %20 between liverpool-ward

Is this because of grunt, or due to something else?

TLDR;

FIXED AS FOLLOWS

selectedValue = selectedValue.replace(/\s+/g, '')

Thanks to: Richard Macarthy and Aaron Digulla for the answer, which led me down the poath to the correct answer.

Just tp be clear, it seems Grunt was adding this whitespace for some reason. The fix is very simple...

ORIGINAL QUESTION

I have an JSON request, which get the contents of a JSON file to be used for data visualisations using d3.js.

This all works fine locally, but when I run grunt build the URL string gets an %20 injected into it from nowhere...

Here is how the string looks before I run Grunt:

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

Which putes to:

http://localhost:8080/app/json/wards-info/liverpool-ward-data.json

After I run Grunt build the puted URL string changes to:

http://localhost:8080/dist/json/wards-info/liverpool%20-ward-data.json

As you can see, it appears to be adding %20 between liverpool-ward

Is this because of grunt, or due to something else?

Share Improve this question edited Apr 7, 2015 at 14:28 Alan asked Apr 7, 2015 at 12:36 AlanAlan 3623 gold badges9 silver badges18 bronze badges 8
  • Well %20 is a whitespace so is there a whitespace in the variable? – epascarello Commented Apr 7, 2015 at 12:38
  • 2 Where does selectedValue e from? It's probably that value that's gaining the extra space/%20. Is it generated by grunt somewhere? – James Thorpe Commented Apr 7, 2015 at 12:38
  • @epascarello There is no whitespace in selectValue – Alan Commented Apr 7, 2015 at 12:41
  • @JamesThorpe It es from an input field, there is no whitespace in the string, I have checked the string before making the request and there is not a space... – Alan Commented Apr 7, 2015 at 12:42
  • 1 Are you positive it is not there? console.log(escape(selectedValue)); And does the s get dropped too? – epascarello Commented Apr 7, 2015 at 12:42
 |  Show 3 more ments

3 Answers 3

Reset to default 6

%20 usually represents a space in HTML URL Encoding, try to make sure there are no spaces in your output.

You can use something like this to help:

string.replace(/ /g,'') to strip the white spaces out. Where string is your URL.

Either that or try this:

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

Simply check your selectedValue value. There is a space before - character. Either remove it, or call trim before using it.

Should solve your problem.

It's probably because of something else. %20 is added because of URL escaping rules (which d3.json() probably applies; Grunt shouldn't have an effect here) but what it means is that selectedValue ends with a space character. I've read in your ments that you're 100% sure that there isn't one but if that was true, then there wouldn't be a %20 in the URL. Computers don't add things just for fun, there is always a reason.

So my suggestion is to debug the code as it runs to see what the variable contains, then search your whole code base for -wards-data.json (because maybe there is a second place in the code that you forgot about).

If that doesn't work, then you'll have to tell us more about the Grunt config (are you pressing scripts, obfuscating? Do you have plugins installed?) Also show us the code which Grunt generates out of your input.

本文标签: jsonJavascript URL string adding 20 (space) after running gruntStack Overflow