0

I have a jQuery AJAX function that saves an image path to the database. Here is an examples parameter:

var data = {};
data['url'] = "Path%20to%20URL";

If there is a space or %20, it will be saved as "Path%20to%20URL" in the database. I haven't changed anything to my code but now, it is saved as "Path+to+URL". Any Idea what's the cause of this?

I already tried to use

str.replaceAll('+', '%20')

in my code just in case it is caused by another function. but no luck.

Here is my jQuery AJAX:

$.ajax({
      url: `server-url`,
      type: 'PUT',
      headers: {
        //auth keys
        'Content-Type': 'application/json'
      },
      data: JSON.stringify(data),
      success: () => {
Michael M.
  • 10,486
  • 9
  • 18
  • 34
TechPro
  • 1
  • 1

1 Answers1

0

"Path%20to%20URL", this is the encoded type of path in HTML. You can use decodeURI() function in this case.

var data = {};

data['url'] = "Path%20to%20URL";
console.log(decodeURI(data['url']));

//"Path to URL"
David F
  • 605
  • 4
  • 10
  • You should always use `decodeURIComponent` for anything that is not an absolute URI. See [MDN for why](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI#decodeuri_vs_decodeuricomponent) – Heretic Monkey Dec 29 '22 at 06:23
  • Also, this doesn't answer the question of why jQuery AJAX is changing `%20` to `+`. – Heretic Monkey Dec 29 '22 at 06:25