Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

js中神奇的出现一个不可见字符,求解决方法

一个上传的地方,

    function upload_init(this_hour_num){
        $('#hour_' + this_hour_num + ' .btn_upload').on({
            click: function(){
                $('#upload_tips').hide();
                data_field = $(this).attr('data-field');
                data_ex = $(this).attr('data-ex');
                $item = $(this);
                /*初始化上传按钮*/
                $('#input_upload').uploadify({
                    "height"          : 30,
                    "multi"          : false,
                    "sizeLimit"       : 500 * 1024 * 1024,
                    "swf"             : "/Public/static/uploadify/uploadify.swf",
                    "fileObjName"     : "download",
                    "buttonText"      : "上传",
                    "uploader"        : "/index.php?s=/course/upload/file_upload/savePath/" + data_field,
                    "width"           : 120,
                    'removeTimeout'   : 1,
                    'fileTypeExts'    : data_ex,
                    "onUploadSuccess" : uploadSuccess,
                    'onFallback' : function() {
                        alert('未检测到兼容版本的Flash.');
                    }
                });

                /*上传成功后方法*/
                function uploadSuccess(file, data) {
//document.write(data);  
                    //var data = $.parseJSON(data);
var data = jQuery.parseJSON(data);
alert(data);
                    if(data.status == 'true') {

代码var data = jQuery.parseJSON(data); 这步出错。原因是多了一个不可见字符导致转码失败。 但是定位又找不到是哪里加的这个字符,所以请表格帮我写个正则,把这个过滤掉
我的想法是在jQuery.parseJSON(data);之前用正则,只取可见字符进行转码(只匹配下面那些就行,用不到的字符全部过滤掉)
大概涉及到的字符是这些:

?{"status":"true","info":{"name":"u4fe1u606fu5b89u5168u5e08u7406u8bbau8003u8bd5u590du4e60u9898(u6574u7406).pdf","type":"courseware","size":297069,"key":"download","ext":"pdf","md5":"cf18eb2947ec4fbad4b1c039f9673585","sha1":"281edac612317cdc8ea8b84ce55df1048a9a25f9","savename":"58c6550dbca94.pdf","savepath":"/Uploads/courseware/2017-03-13/","userid":false,"create_time":1489392909,"file_id":31476}}

js实在渣,写了半天写不对,不知道是写的代码有问题还是这样的思路不对。求表哥帮忙写一个


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
let data = '?{"status":"true","info":{"name":"u4fe1u606fu5b89u5168u5e08u7406u8bbau8003u8bd5u590du4e60u9898(u6574u7406).pdf","type":"courseware","size":297069,"key":"download","ext":"pdf","md5":"cf18eb2947ec4fbad4b1c039f9673585","sha1":"281edac612317cdc8ea8b84ce55df1048a9a25f9","savename":"58c6550dbca94.pdf","savepath":"/Uploads/courseware/2017-03-13/","userid":false,"create_time":1489392909,"file_id":31476}}'
let json = JSON.parse(data.trim())
console.log(json)

试试

var data = jQuery.parseJSON(data.trim());

这个字符出现的原因可以参考这里进行排除


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...