Difference in URL decode/encode UTF-8 between Java and JS/AS3 (bug!?)

后端 未结 3 705
无人及你
无人及你 2021-01-06 00:50

I am having an issue URL decoding a UTF-8 string in Java that is encoded either with Javascript or Actionscript 3. I\'ve set up a test case as follows:

The string in

3条回答
  •  感动是毒
    2021-01-06 01:34

    I have been struggling with this problem for hours on end... My problem was a JQuery Ajax call like:

    return $.ajax({
            url: '/author!getAuthorContent.action',
            type: 'GET',
            data : {author:name, 'content_type': ct || 'all', 'start': start || 0}
        });
    

    'name' is a String which contains special characters like Jérôme-Serrano

    For some reasons the way JS/JQuery was encoding these kind of special characters was incompatible and I couldn't decode it on Java BackEnd...

    The solution was:

    • Encode on JS side using var econded = encodeURIComponent(name);
    • Decode them on Java side using String decoded = java.net.URLDecoder.decode(econded ,"UTF-8");

    some refetences: http://www.programering.com/a/MjN2ADOwATg.html http://www.theerrormessage.com/2013/10/weird-characters-transmitted-to-and-from-server-through-jquery-ajax-call/

提交回复
热议问题