简体   繁体   English

如何将字符串数据编码为UTF编码格式

[英]How to encode the string data into UTF encoded format

I am passing some parameters from UI to backend API. 我正在将一些参数从UI传递到后端API。 But, I want to encode those params before sending to backend. 但是,我想在发送到后端之前对这些参数进行编码。

I thought to use function encodeURIComponent() , but that will not help, as it is used for encoding the URLs. 我以为可以使用函数encodeURIComponent() ,但这无济于事,因为它用于编码URL。

dataInfo contains all the params that will be passed to backend. dataInfo包含将传递给后端的所有参数。

        var dataInfo = {};
        dataInfo.pId = "584e88f472f94906b09e04a8";
        dataInfo.aId = localStorage.aId;
        dataInfo.fName = fName;
        dataInfo.fJson = fJson;
        dataInfo.userName = localStorage.username;

and dataInfo is getting passed to data while calling backend API. 并且dataInfo在调用后端API时传递给数据。

    $.ajax({
            type: "POST",
            url: localStorage.idataInfoApi,
            data: JSON.stringify(dataInfo),
            contentType: "text/html",
            dataType: "html",
        });

Please guide me on how can I encode the complete dataInfo into UTF encoded format, so that resulting data will be in byte[] . 请指导我如何将完整的dataInfo编码为UTF编码格式,以使结果数据以byte[]

I hope this will answer your query. 我希望这能回答您的查询。 The best option is to use encodeURIComponent . 最好的选择是使用encodeURIComponent May be your missing content type. 可能是您缺少的内容类型。

$.ajax({
        type: "POST",
        url: localStorage.idataInfoApi,
        data:"q="+encodeURIComponent( dataInfo ),
        scriptCharset: "utf-8" ,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM