UTF-8 string not decoded correctly in AngularJS

后端 未结 2 1830
执念已碎
执念已碎 2020-12-20 12:04

I have a text input box, within a SPA built on AngularJS, for users to add a title to a printout. The input box is declared like this:



        
2条回答
  •  猫巷女王i
    2020-12-20 12:43

    Here is your issue solved.

    Based on this source,

    UTF-8 character debugging and its encoding and decoding

    The response you are getting is the actual charecter of the encoded utf-8 string

    So, you need to decode that inorder to get your result.

    HEre is the code to do it.

        decoded =  decodeURIComponent('%C3%A0%C3%9F%C3%A9%C3%A7%C3%B8%C3%B6')
    
        console.log(decoded);
    
       The result is => "àßéçøö"
    

    we have to do this to get the actual string instead of UTF-8

    So, from your response you got,à Ãéçøö

    decodeURIComponent(escape("à Ãéçøö")) => "àßéçøö"

    DEFINITION:

    decodeURIComponent():

    • A new string representing the decoded version of the given encoded Uniform Resource Identifier (URI) component.

    So , here is your method.

    if (status == 200) {
        var original = headers("charttitle");
        var chartTitle = decodeURIComponent(escape(original));
        console.log(chartTitle);
        var printoutInformation = {'chartTitle' : chartTitle, 'pdfData' : data};
        deferred.resolve(printoutInformation);
    }
    

    Now, you will get the headers same as you send.

提交回复
热议问题