javascript - Upload failed token not specified

WBOY
Release: 2016-07-06 13:54:11
Original
3519 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!