0

I need to pass the '+' character with ajax parameter to my controller.

Ajax Call with parameter contain '+' charactor.

var subsNumbers = '+94'
var url = 'getList?subsNums='+subsNumbers;

$.ajax({
        url:url,
        type:'POST',
        dataType:'json',
        success:function (saveResponse) {
....
}
});

In my controller(Spring controller class),

String deviceNumbers = request.getParameter("subsNums");
logger.debug("deviceNumbers-->{}", deviceNumbers);

the '+' character has been replaced with space. Actual result is

deviceNumbers--> 94 

Expected is

deviceNumbers-->+94 
Dinesh Appuhami
  • 710
  • 1
  • 11
  • 24
  • your ajax dataType is `JSON` but your sending a string right? and for your question I guess it can be solved by **Url Encode @Client** and **Url Decode @Server**. – yashhy Jan 02 '14 at 11:38
  • @Yashhy http://stackoverflow.com/questions/13735869/datatype-application-json-vs-json – Musa Jan 02 '14 at 11:41
  • @Musa thanks that was well explained!! :) – yashhy Jan 02 '14 at 11:52

1 Answers1

6

Your url is not properly encoded, use encodeURIComponent

var url = 'getList?subsNums='+encodeURIComponent(subsNumbers);
Musa
  • 96,336
  • 17
  • 118
  • 137
  • 2
    to explain why: the + character in a URL is by convention a substitute for the illegal space character, so if your URL contains + symbols, the receiver MUST interpret them as "these are really spaces". – Mike 'Pomax' Kamermans Jan 02 '14 at 11:40