简体   繁体   English

XMLHttpRequest始终返回未定义

[英]XMLHttpRequest always returns undefined

I am making an XMLHttpRequest that should return an mp3 file. 我在做一个XMLHttpRequest应该返回一个mp3文件。 However, it returns ' undefined '. 但是,它返回“ undefined ”。 I am not getting any errors in the console (thought it would be a allow-content issue but this is a public API). 我在控制台中没有收到任何错误(认为这是一个允许内容的问题,但这是一个公共API)。

This is the jssfiddle and the code. 这是jssfiddle和代码。 Please note that I am not posting a valid API key so we should receive " ERROR: The API key is not available! " instead of the mp3, but still not an undefined . 请注意,我没有发布有效的API密钥,因此我们应该收到“ ERROR: The API key is not available! ”而不是mp3,但仍然不是undefined

https://jsfiddle.net/r0j9yojo/5/ https://jsfiddle.net/r0j9yojo/5/

<button onclick="doit()">doit</button>

function doit() {
    var endpoint = 'https://api.voicerss.org/?key=testkey&src=test&hl=es-es';
    var client = new XMLHttpRequest();
    client.open('GET', endpoint, true);
    client.onreadystatechange = function() {
      if (client.readyState == 4 && client.status == 200) {
    alert(client.readyState + '/' + client.status + '/' + client.responsetext);
      }
    }
    client.send();
}

The property of client you are looking for is responseText , not responsetext (note the capitalized T). 您要查找的client的属性是responseText ,而不是responsetext (请注意大写的T)。

In the future, use console.log(object) (in your case console.log(client) ) to inspect an object through your browsers console, which is usually accessed with F12 or through the menu. 将来,请使用console.log(object) (在您的情况下为console.log(client) )通过浏览器控制台检查对象,通常通过F12或菜单访问该对象。

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

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