0

I am communicating to a REST server which is expecting something like http://example.com/1.0/editor/5/bla/26 along with body parameters of x, y, and z. Or maybe instead of /1.0/editor/5/bla/26, it should be 1.0/editor/5/bla/what about item #26, but of course with what about item #26 escaped.

There are many post describing how to use jQuery.param() to encode parameters, however, that is not my question. How chould the actual url be created using jQuery of JavaScript?

$.ajax({
    type:'PUT',
    url:'/1.0/editor/'+$('#id').val()+'/bla/'+$('#wha').data('id'),
    data:{x:x,y:y,z:z},
    success: function (error){}
});
user1032531
  • 24,767
  • 68
  • 217
  • 387
  • What's wrong with what you have? URLs are strings, so string concatenation is a standard way of creating them. – Heretic Monkey Sep 12 '16 at 16:58
  • @MikeMcCaughan So, an unescaped url of `1.0/editor/use number 5!/bla/what about item #2` is okay? – user1032531 Sep 12 '16 at 17:07
  • No, you'd need to escape/encode them, as you already said. To encode strings in a URI, you'd use encodeURIComponent() on the string. See the duplicate. – Heretic Monkey Sep 12 '16 at 17:12
  • @MikeMcCaughan I don't think `encodeURIComponent()`, but just `encodeURI()`. Maybe `encodeURI('1.0/editor/use number 5!/bla/what about item #2')`? – user1032531 Sep 12 '16 at 17:15

1 Answers1

0

Can you write a javascript RegEx function to replace all spaces with a known character? Or replace it with its HTML equivalent. Then in your backend, just look for that escape character and replace them with the spaces again. It's kind of a work around but depending on your situation it could work.

ccopland
  • 89
  • 4