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

1
  • You both gave the right response but stackoverflow just allow me to accept one. Thank you again Commented Oct 1, 2014 at 12:11

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.