Internet Explorer 8 中提示文件下载时原型 Ajax.Request 出现问题

发布于 2024-09-28 01:50:42 字数 1036 浏览 0 评论 0 原文

拥有一组支持原型的 ajax 代码,可以在 IE 以外的所有浏览器中运行。在 IE8 中,JSON(否则会返回到 Ajax.Request 中指定的 onSuccess 处理函数)会被扔到文件下载流中,该流会弹出并提示下载位置。

askForm = $('askForm');
var askUrl = '.';
var askParameters = askForm.serialize(true);
askForm.disable();

var askAjax = new Ajax.Request(
    askUrl, {
    method: 'post', 
    parameters: askParameters, 
    onSuccess: handleResults,
    onFailure: handleError
    }
);

function handleError(transport) {
   alert('Please refresh this page, an error occurred in processing at the server.');
}

function handleResults(transport) {
...
}

handleResults 函数中有更多代码,但永远不会被调用。经调试,调用Ajax.Request函数时出现下载提示。

文件名IE8每次都会提示下载更改,4个看似随机的十六进制值(8个字符),没有文件扩展名。文件的内容是来自服务器的纯 JSON 响应...

{"question": ["Enter your question*"], "name": ["Enter your name (First L.)*"], "sender": ["Enter your e-mail*"]}

非常感谢这里的任何提示。这种情况发生在雪豹上,在 VMWare Fusion 中运行 IE8,访问 OS X 上通过 apache/django/python 运行的站点。但是,由于 VMWare Windows XP 计算机中的 Chrome 和 Firefox 运行正常,似乎直接指出 IE8 是罪魁祸首。

Have a set of prototype-enabled ajax code that is working in all browsers other than IE. In IE8 the JSON, that otherwise gets returned to the onSuccess handler function specified in Ajax.Request, gets thrown into a file download stream which pops up and prompts for where to download.

askForm = $('askForm');
var askUrl = '.';
var askParameters = askForm.serialize(true);
askForm.disable();

var askAjax = new Ajax.Request(
    askUrl, {
    method: 'post', 
    parameters: askParameters, 
    onSuccess: handleResults,
    onFailure: handleError
    }
);

function handleError(transport) {
   alert('Please refresh this page, an error occurred in processing at the server.');
}

function handleResults(transport) {
...
}

There is more code in the handleResults function but this never gets called. Having debugged, the download prompt occurs when the Ajax.Request function is called.

The filename IE8 prompts to download changes each time, 4 seemingly random hex values (8 characters) with no filename extension. And the contents of the file are the pure JSON response from the server...

{"question": ["Enter your question*"], "name": ["Enter your name (First L.)*"], "sender": ["Enter your e-mail*"]}

Would be much obliged for any tips here. This is occurring on Snow Leopard with IE8 running in VMWare Fusion accessing a site running via apache/django/python on OS X. However, since Chrome and Firefox in the VMWare Windows XP machine function properly, seems to point directly to IE8 as the culprit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕梦 2024-10-05 01:50:42

这个问题已经解决了。这里的问题很疯狂,但事实证明,存在一个 javascript 错误,导致脚本无法执行,并且标准表单提交是通过浏览器发生的,从而返回 AJAX 代码作为文件下载提示。该表单的设计方式使得禁用 javascript 的浏览器仍然能够使用名为“js”的隐藏输入字段而无需使用 ajax 的表单。通过 AJAX 提交时,JavaScript 会将此字段的值留空,让服务器知道响应应该是 JSON,而不是整个页面刷新。好吧,javascript 中清空 js 字段值的部分正常处理,但随后脚本出错,因此 event.stop() javascript 从未执行。导致表单被处理为标准的“提交”按钮单击、通过浏览器的 POST 请求。

请注意上面的代码...

askForm = $('askForm');

显然应该是...

var askForm = $('askForm');

感谢允许此语法的浏览器,但结果却让我陷入了 IE8 的白费力气。一直在学习。

This problem has been solved. Crazy issue here but it turns out that there was a javascript error that was preventing the script from executing and the standard form submit was occurring thru the browser thus returning AJAX code as a file download prompt. The form was designed in such a way that javascript-disabled browsers would still be able to use the form without ajax using a hidden input field with name "js". The javascript would blank the value of this field when submitting via AJAX to let server know the response should be JSON and not a full page refresh. Well, that part of the javascript that blanked the js field value processed normally but then the script errored out and thus the event.stop() javascript never executed. Resulting in the form being processed as a standard Submit button click, POST request through browser.

Note in the code above...

askForm = $('askForm');

which clearly should be...

var askForm = $('askForm');

Thanks to the browsers that allowed this syntax but it sent me on an IE8 wild goose chase as a result. Always learning.

恋你朝朝暮暮 2024-10-05 01:50:42

将 JSON 发送到浏览器的服务器端文件是否发送了正确的标头?如果它不将自己标识为 JSON,浏览器可能会将其视为要下载的文件。

Does the server side file that sends the JSON to the browser send the proper headers? If it doesn't identify itself as JSON the browser might consider it a file to be downloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文