6

I use Angularjs to send a get http request to my server. The server respond to rest request using Spring MVC. Here a snippet code on my angular url building :

var name="myname";
var query= "wo?d";
var url = "/search/"+query+"/"+name;

Here Spring MVC Controller :

@RequestMapping( value = "/search/{query}/{name}" , method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_VALUE )
@ResponseBody
public List<Data> search() {

    // search logic

return data;
}

I have the problem when the query contains question mark character ( ? ). The url is split so that Spring responds : WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/my-server/web/search/wo in DispatcherServlet with name 'mvc'

This is right because the question marks ? introduce the begining of request parameters. I've tried to use the js function encodeURI(query) but the question mark character is not encoded. How to encode a question mark in url ? Thanks

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
Pracede
  • 4,226
  • 16
  • 65
  • 110
  • You both gave the right response but stackoverflow just allow me to accept one. Thank you again – Pracede Oct 01 '14 at 12:11

2 Answers2

9

Javascript has special function for this purpose encodeURIComponent

encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )

while encodeURI also does not encode ; , / ? : @ & = + $ # characters.

fauverism
  • 1,970
  • 3
  • 33
  • 59
dfsq
  • 191,768
  • 25
  • 236
  • 258
4

Use encodeURIComponent for that.

check this fiddle.

var a = "wo?d";
alert(encodeURIComponent(a));

For angular specific look this SO answer.

still some problem then post me.

Community
  • 1
  • 1
user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62