HTml5文件Api:CrossDomain问题

时间:2013-11-22 11:11:46

标签: javascript html5 xmlhttprequest cross-domain fileapi

我在使用文件Api时遇到跨域问题。当我更改变量photoUrl的值时它工作正常。这是代码:

   window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;

var xhr = new XMLHttpRequest();
var photoUrl = 'http://dreams/images/namelogo.png';///Working code is: var photoUrl="images/namelogo.png";
xhr.open('GET', photoUrl, true);
xhr.overrideMimeType('text/plain; charset=x-user-defined');

function stringToBinary(response) {
var byteArray = new Uint8Array(response.length);
for (var i = 0; i < response.length; i++) {
 byteArray[i] = response.charCodeAt(i) & 0xff;
}
return byteArray
}

function onInitFs(fs) {
 xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
  fs.root.getFile('image.jpg', {'create': true}, function(fileEntry) {
    fileEntry.createWriter(function(fileWriter) {
      fileWriter.onwriteend = function(event) {
       // $('body').append('<img src="' + fileEntry.toURL() + '"/>');
      }

      buffer = stringToBinary(xhr.response);
      var blob = new Blob([ buffer ], { type: 'image/jpeg' } )

      fileWriter.write(blob);
    }, errorHandler );
  });
}
}
xhr.send();

 }

  var errorHandler = function(err) {
   console.log(err);
}
$(function() {
   webkitStorageInfo.requestQuota(PERSISTENT, 5*1024*1024, function(grantedBytes) {
  requestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler)
 }, errorHandler)
 })

上面的代码显示以下错误:

“XMLHttpRequest无法加载。    请求的资源上不存在“Access-Control-Allow-Origin”标头。     因此不允许原点访问“

但是当我将photoUrl值更改为“images / namelogo.png”时,它运行正常。我该如何解决这个问题?

由于

0 个答案:

没有答案
相关问题