同樣的代碼
在www.a.com 下上傳正常
在www.b.com 下,相同的代碼,通過www.a.com 拿到token 上傳報錯 error: "token not specified
uptoken_func 下uptoken 返回了空值之後才被 ajax 回調過來的資料賦值,所以上傳的時候 token 為空白
var qiniu_up = qiniu.uploader({ runtimes : 'html5,flash,html4', browse_button : 'qiniu_browse', get_new_uptoken: true, unique_names: false, save_key : true, domain : '', container : 'container', max_file_size: '4mb', flash_swf_url : '', silverlight_xap_url : '', 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 : '', 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; } });
回複內容:
同樣的代碼
在www.a.com 下上傳正常
在www.b.com 下,相同的代碼,通過www.a.com 拿到token 上傳報錯 error: "token not specified
uptoken_func 下uptoken 返回了空值之後才被 ajax 回調過來的資料賦值,所以上傳的時候 token 為空白
var qiniu_up = qiniu.uploader({ runtimes : 'html5,flash,html4', browse_button : 'qiniu_browse', get_new_uptoken: true, unique_names: false, save_key : true, domain : '', container : 'container', max_file_size: '4mb', flash_swf_url : '', silverlight_xap_url : '', 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 : '', 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; } });
看樓主的代碼 好像是往七牛CDN上面上傳檔案
token的問題 樓主可以看看是不是JS跨域問題 你也可以看看七牛的開發文檔 或者 聯絡七牛的技術人員聯調一下
uptoken_func 下的ajax 跨域了以後他的運行順序變了 所有的程式都執行完了才執行到success 下的uptoken 的被賦值,所以上傳上去的uptoken 為空白