Home >Backend Development >PHP Tutorial >javascript - Upload failed token not specified

javascript - Upload failed token not specified

WBOY
WBOYOriginal
2016-07-06 13:54:113571browse

The same code
Uploading normally under www.a.com
Under www.b.com, the same code, getting the token through www.a.com and uploading error: "token not specified"

Uptoken under uptoken_func returns a null value before being assigned a value by the data called back by ajax, so the token is empty when uploading

<code>
var qiniu_up = qiniu.uploader({
            runtimes : 'html5,flash,html4',
            browse_button : 'qiniu_browse',
            get_new_uptoken: true,
            unique_names: false,
            save_key : true,
            domain : '<?php echo QINIU_BUCKET_SITE;?>',
            container : 'container',
            max_file_size: '4mb',
            flash_swf_url : '<?php echo RES_ROOT.'/assets/js/plupload/Moxie.swf';?>',
            silverlight_xap_url : '<?php echo RES_ROOT.'/assets/js/plupload/Moxie.xap';?>',
            max_retries: 1,
            dragdrop: false,
            drop_element : 'container',
            chunk_size : '4mb',
            auto_start: false, /*不开启自动上传*/
            multi_selection: false, /*设置为只能选择单个文件*/
            filters : {
                mime_types : [{title : 'Image files', extensions : 'jpg,jpeg,gif,png,bmp'},{title : 'Voice files', extensions : 'mp3,wav'}]
            },
            init : {

            },
            uptoken_func: function(file){
              var uptoken = '';
              var media_info = '[{"directory": "1","directory_class": "","up_file_name": "'+file.name+'","media_type": "1"}]';
                 $.ajax({
                     type : "post",
                     async : false, /*必须使用同步*/
                     url : '<?php echo WEBSITE_URL.'/qiniu_upload/media_uptoken';?>',
                     data : {
                         app_version : '1.0',
                         app_target : 3,
                         sdk_version : '1.0',
                         plat : 'other',
                         media_info : media_info
                     },
                     dataType : "jsonp",
                     jsonp: "jsonpCallback",
                     success : function(data){
                         if(data.code == '1') {
                             var list = data.data.uptoken_list;
                             uptoken = list[0]['uptoken'];
                             media_id = list[0]['media_id'];
                         } else {
                             alert(data.message);
                         }
                     },  
                     error : function(XMLHttpRequest, textStatus, errorThrown){  
                        
                     }  
                 });
                return uptoken;
            }
        });
</code>

Reply content:

The same code
Uploading normally under www.a.com
Under www.b.com, the same code, getting the token through www.a.com and uploading error: "token not specified"

Uptoken under uptoken_func returns a null value before being assigned a value by the data called back by ajax, so the token is empty when uploading

<code>
var qiniu_up = qiniu.uploader({
            runtimes : 'html5,flash,html4',
            browse_button : 'qiniu_browse',
            get_new_uptoken: true,
            unique_names: false,
            save_key : true,
            domain : '<?php echo QINIU_BUCKET_SITE;?>',
            container : 'container',
            max_file_size: '4mb',
            flash_swf_url : '<?php echo RES_ROOT.'/assets/js/plupload/Moxie.swf';?>',
            silverlight_xap_url : '<?php echo RES_ROOT.'/assets/js/plupload/Moxie.xap';?>',
            max_retries: 1,
            dragdrop: false,
            drop_element : 'container',
            chunk_size : '4mb',
            auto_start: false, /*不开启自动上传*/
            multi_selection: false, /*设置为只能选择单个文件*/
            filters : {
                mime_types : [{title : 'Image files', extensions : 'jpg,jpeg,gif,png,bmp'},{title : 'Voice files', extensions : 'mp3,wav'}]
            },
            init : {

            },
            uptoken_func: function(file){
              var uptoken = '';
              var media_info = '[{"directory": "1","directory_class": "","up_file_name": "'+file.name+'","media_type": "1"}]';
                 $.ajax({
                     type : "post",
                     async : false, /*必须使用同步*/
                     url : '<?php echo WEBSITE_URL.'/qiniu_upload/media_uptoken';?>',
                     data : {
                         app_version : '1.0',
                         app_target : 3,
                         sdk_version : '1.0',
                         plat : 'other',
                         media_info : media_info
                     },
                     dataType : "jsonp",
                     jsonp: "jsonpCallback",
                     success : function(data){
                         if(data.code == '1') {
                             var list = data.data.uptoken_list;
                             uptoken = list[0]['uptoken'];
                             media_id = list[0]['media_id'];
                         } else {
                             alert(data.message);
                         }
                     },  
                     error : function(XMLHttpRequest, textStatus, errorThrown){  
                        
                     }  
                 });
                return uptoken;
            }
        });
</code>

It seems that the poster’s code is uploading files to Qiniu CDN
Regarding the token problem, the poster can check whether it is a JS cross-domain issue. You can also look at Qiniu’s development documents or contact Qiniu’s technical staff. Adjust it

After the ajax under uptoken_func cross-domain, its running order has changed. All programs have been executed before the uptoken under success is assigned a value, so the uploaded uptoken is empty

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn