javascript - 无法在 dojo/request/iframe post 上读取未定义的属性 'value'

标签 javascript asp.net-mvc-4 iframe dojo image-upload

我有一个图片上传功能,可以像这样上传带有图片的表单:

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/

相关文章:

javascript - 如何将 iframe 面板注入(inject) Google Chrome 扩展并在 iframe 内添加点击事件?

javascript - jQuery - 在 iframe 内按钮的 onclick 事件中更改 iframe 高度

javascript - scrolltop() 在 IE9 中根本不起作用

javascript - 如何延迟每个项目的 Bootstrap 轮播?

c# - 如何在 DropDownFor 的 SelectList 中设置所选项目?

javascript - 分页未移动到kendogrid中的新数据

javascript - 何时使用 iframe、ajax() 或 load()?

javascript - 另一个函数发出请求后的 jQuery 回调

javascript - 禁用所有单击/按键事件,直到 ajax 调用完成

c# - 返回验证错误