简体   繁体   English

状态码304

[英]Status Code 304

I am new to Javascript and server-side programming. 我是Java和服务器端编程的新手。 I am trying to send a GET request to load an image from my blog: http://jsafaiyeh.github.io/img/suw_background.png 我正在尝试发送GET请求以从我的博客加载图像: http : //jsafaiyeh.github.io/img/suw_background.png

function imgLoad(url) {
return new Promise(function(resolve, reject) {

  var request = new XMLHttpRequest({mozSystem: true});
  request.open('GET', url);
  request.responseType='blob';

  request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
      resolve(request.response);
    } else {
      reject(Error('Image did\'t load successfully; error code '+ request.statusText));
    }
  };

  request.onerror= function() {
    reject(Error('There was a network Error'));
  };
  request.send();
});
}

var body = document.querySelector('body');
var myImage = new Image();

imgLoad('http://jsafaiyeh.github.io/img/suw_background.png').then(function response() {
  var imageURL = window.URL.createObjectURL(response);
  myImage.src = imageURL;
  body.appendChild(myImage);
}, function(Error) {
  console.log(Error);
});

I get status code 304. However, the image still does not load onto the page. 我收到状态代码304。但是,该图像仍未加载到页面上。 Any help would be appreciated. 任何帮助,将不胜感激。

You have wrong function signature. 您的函数签名错误。 It should be like this: 应该是这样的:

imgLoad('http://jsafaiyeh.github.io/img/suw_background.png').then(function (response) {
  var imageURL = window.URL.createObjectURL(response);
  myImage.src = imageURL;
  body.appendChild(myImage);
}, function(Error) {
  console.log(Error);
});

Working demo on JSFiddle (at least in Chrome). 在JSFiddle上运行演示 (至少在Chrome中)。

Instead of passing named function, called response you probably wanted response to be in argument list. 可能不希望将response传递到参数列表中,而不是传递命名函数(称为response So, instead of function response() , you need function (response) . 因此,您需要function (response)来代替function response() function (response) You didn't get error that response was undefined, because it actually was declared, but it wasn't expected result from promise, but function. 您没有收到未定义response错误信息,因为它实际上是已声明的,但是它不是来自promise的预期结果,而是函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM