官方demo的代码,替换成自己的用nodejs sdk生产的token,也会出这个错误(node端可以上传文件)。在网站
http://jsfiddle.net/gh/get/ex...上生成的token,粘帖过来也会报错。请各位指点下。
var UpToken = "UpToken "+token; //token 来自服务器,nodejs sdk生成。生产的token在node上,可以上传文件。不会报错。
var xhr = new XMLHttpRequest();
xhr.onreadystatechange=function(){
if (xhr.readyState==4){
document.getElementById("myDiv").innerHTML=xhr.responseText;
}
}
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.setRequestHeader("Authorization", UpToken);
xhr.send(test);
uptoken是nodejs sdk生产的。
var qiniu= require('qiniu');
var http = require("http");
var url=require('url');
qiniu.conf.ACCESS_KEY = 'wDO9CoQ8bfd2rR7Z3fpoirbvxm5xrEVJjMMLpU52';
qiniu.conf.SECRET_KEY = 'kh2s9LnYPAYD0nTGR3eStGVV1H1uQVmvkO68MjDQ';
var bucket ="landslides";
var key = 'test';
function uptoken(bucket, key) {
var putPolicy = new qiniu.rs.PutPolicy(bucket+":"+key);
return putPolicy.token();
}
http.createServer(function(req, response) {
var token = uptoken(bucket,key);
console.log("server");
response.setHeader("Access-Control-Allow-Origin", "*");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(token);
response.end();
}).listen(8888);
key的问题,你的服务端那里指定了key是test,你上传的url中应该是:
http://upload.qiniu.com/put64/-1/key/<test的base64编码>