Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将本机JS Ajax片段转换为JQuery替代方案?_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 将本机JS Ajax片段转换为JQuery替代方案?

Javascript 将本机JS Ajax片段转换为JQuery替代方案?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在更新另一位作者的历史JS代码,但在将以下代码转换为JQuery替代代码时遇到问题: var xhr = new XMLHttpRequest(); xhr.open('GET', songurl, true); xhr.responseType = 'blob'; xhr.onload = function(e) { if (this.status == 200) { document.getElementById('songdiv').innerHTML = ''; so

我正在更新另一位作者的历史JS代码,但在将以下代码转换为JQuery替代代码时遇到问题:

var xhr = new XMLHttpRequest();
xhr.open('GET', songurl, true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
    document.getElementById('songdiv').innerHTML = '';
    song.src = (window.webkitURL ? webkitURL : URL).createObjectURL(this.response);
}
}
xhr.send();
更重要的是,为了帮助保持一致性,Chrome-developer工具还建议对代码进行重新分解

这是我开始做的(感谢这不多!),我遇到的问题是检查状态代码,如果状态代码是200,则返回响应

$.ajax({
    url: songurl,
    method: 'GET'
);
您要附加

ajax()
,或者简称,将为您完成所有这些。有一个
success()

$.get( songurl, 
  function(data) {
    document.getElementById('songdiv').innerHTML = '';
    song.src = (window.webkitURL ? webkitURL : URL).createObjectURL(data);
  },
  'blob'
);

@A.Wolff只是明确表示未使用的
数据
参数。不过没必要。。。我将删除它。哦,但通常更好的做法是明确地…:)我想知道这一点,因为我不熟悉blob,它可能有一个特定的原因。感谢响应,学到了一些新的东西……尽管遗憾的是JQuery默认不支持blob!谢谢你,我在两个选项之间选择了一个,但是由于额外的评论,我接受了另一个。我很惊讶你接受了另一个,因为它使用了与你最初使用的不同的功能。
$.get( songurl, 
  function(data) {
    document.getElementById('songdiv').innerHTML = '';
    song.src = (window.webkitURL ? webkitURL : URL).createObjectURL(data);
  },
  'blob'
);