-1

I have tried the following line in my ajax call as it was suggested all over internet:

contentType: "application/json; charset=ISO-8859-1"

but it did not change anything. Whenever I make a call from ajax with a string parameter which has an accent for example società, at my controller(I'm using Java, spring framework), i receive it as SocietÃ. I have tried first UTF-8 which is the default (as far as I know) but nothing. Here is my full function:

function foo(customer, society) {
    $.ajax({
        url: 'notMappedAcoStaffing.do',
        type: "GET",
        contentType: "application/json; charset=ISO-8859-1",  
        data: { customer: customer, society: society},
        success: function(data) {   $('#content').html(data);    },
        error: function(error) { alert("error" + error); }
    });
}

And I take the values on my backend as

@RequestMapping(method = RequestMethod.GET)
public ModelAndView fooController(@RequestParam("customer") final String customerParam, @RequestParam(value="society",required=false) Integer societyParam) {...}

Is there a way to resolve this issue? Thanks for your help in advance.

UPDATE: I have still no idea why I receive it distorted as parameter at my controller. But however when I convert it as follows: How do I convert between ISO-8859-1 and UTF-8 in Java? ,it comes out all good. Seeing that it works, I tried it again to set UTF-8 in my ajax call but no success... It resolved my issue but honestly I do not like this way. Please share with me if you have a better solution maybe on the ajax call side. Thanks

Red fx
  • 1,071
  • 2
  • 12
  • 26
  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help), in particular [How do I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](/help/on-topic). From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." even if it is no homework. – Timothy Truckle Sep 11 '17 at 08:32

1 Answers1

1

Part of your problem is that you're using HTTP GET, which basically means that your data will be embedded in the URL itself, in the query string ( http://example.org/?customer=foo&society=bar ). This can lead to problems when using special characters.

Especially, the contentType property concerns the body of the request which you do not have at all so currently it has little impact.

Consider sending the data with HTTP POST (and remove your custom contentType) which gives you more power over the encoding of your data - which is then sent in the request body instead of the URL. It might solve your problem outright, or you might need to configure your server to use the same encoding as the client - I suggest UTF-8 for both parties.

jlaitio
  • 1,878
  • 12
  • 13
  • Thank you a lot. I have been trying also Post but it wasnt working, I tried also using JSON.stringify.. But I was leaving the contentType there. As I have removed it, it started to work just so smooth and fine. Thanks again ! – Red fx Sep 11 '17 at 10:51