我有一个图片上传功能,可以像这样上传带有图片的表单:
iframe(url, {
form: dom.byId("myform"),
handleAs: "json",
timeout: 5000,
method: "POST"
}).then(function () {
console.log("Success");
}, function (Err) {
console.log(Err);
});
在服务器端我得到图像,但在客户端我得到 TypeError: Cannot read property 'value' of undefined↵ at I [as handleResponse] ( http://ajax.googleapis.com/ajax/libs/dojo/1.9.2/dojo/request/iframe.js:9:114 )↵ at r ( http://ajax.googleapis.com/ajax/libs/dojo/1.9.2/dojo/dojo.js:206:81 )”。我没有返回值!我不明白导致错误的原因! 请帮忙!
最佳答案
来自 Dojo Reference Guide :
Important: If your payload is something other than html or xml (e.g. text, JSON) the server response needs to enclose the content in a
<textarea>
tag. This is because this is the only cross-browser way for this provider to know when the content has been successfully loaded. Therefore the server response should look something like this:
<html>
<body>
<textarea>
payload
</textarea>
</body>
</html>
解决此问题所需要做的就是将 JSON 响应包装在 <textarea>
中标签。原因在iframe.js
,从第 300 行开始:
if(handleAs === 'xml'){
...
}else{
// 'json' and 'javascript' and 'text'
response.text = doc.getElementsByTagName('textarea')[0].value; // text
}
所以这里是您收到错误消息的地方,“无法读取 undefined
的属性‘值’。” Dojo 找不到 <textarea>
你的回应中的元素,所以doc.getElementsByTagName('textarea')
返回一个空数组。空数组的第 0 个元素,[]
, 是 undefined
,并取消引用它会引发此错误。
关于javascript - 无法在 dojo/request/iframe post 上读取未定义的属性 'value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22919643/