XMLHttpRequest的状态为200,但responseText为空
XMLHttpRequest has a 200 status, but responseText is empty
我正在制作一个XMLHttpRequest来上传一个图像文件。我可以看到这是一个200的状态,我的图片正在上传到我的服务上。然而,当我使用下面的代码来获取responseText(它应该包括诸如:我的图像的URL、文件名、时间戳、图像尺寸等信息)时,它会返回一个空字符串:
//...
const data = new FormData();
data.append('file', props);
data.append('upload_preset', uploadPreset);
data.append('api_key', apiKey);
const xhr = new XMLHttpRequest();
xhr.open('POST', cloudinaryURL, true);
const sendImage = xhr.send(data);
const imageResponse = xhr.responseText;
console.log('Response: ', imageResponse);
将此打印到控制台:
Response:
知道为什么会发生这种情况/如何解决这个问题吗?
谢谢!!
您在异步模式下使用XMLHttpRequest:
xhr.open('POST', cloudinaryURL, true);
但您的代码是为同步模式设计的:
xhr.open('POST', cloudinaryURL, false);
在异步模式下,xhr.send()
将立即返回,而不会实际处理请求。因此,当您尝试访问xhr.responseText
时,它还没有被填充。
相关文章:
- 如果文本字段为空,则使用JavaScript应用CSS样式
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 如果localstorage为空,则显示欢迎消息
- 使用 jQuery 检查所有值是否为空或已填充
- 如果值为空,如何设置输入的默认值?jQuery
- 如何检查元素的内容是否为空,如果为空,请在jquery中删除该元素
- 在MVC 4中,对Controller的Ajax调用为空
- Spring Ajax列表返回,但对象为空
- 输入文本框为空时的阻止按钮asp.网络表单
- $_POST变量为空
- 如果数据为空,Ajax加载不会停止
- 边框颜色是't如果输入为空,则更改
- servlet-为什么XMLHttpRequest responseText始终为空
- GM_xmlhttpRequest responseText在Firefox中为空,但在Chrome中是完美的
- ajax GET请求responseText为空
- responseText为空,但显示在控制台上
- 响应XML为空,尽管responsetext是有效的.net webservice
- XMLHttpRequest状态为4,但responseText为空
- xmlhttprequest中的responseText为空
- XMLHttpRequest的状态为200,但responseText为空