0

I was trying to submit a HTML form that includes text fields and an image file using AJAX and jQuery. So it turns out you cannot do file upload using AJAX. Are there any nice ways to submit text form data along with a file? I'm aiming for broad browser compatibility, particularly with older IE versions.

One way I've tried is to eliminate using AJAX and simply use normal POST (although AJAX is the preferred way). After the POST another page loads. I can redirect the browser to load another page from my PHP using

header("Location: new_page.html");

but after this, how can I send the JSON response to the JavaScript script on new_page.html. If I do something like this, will the response now go to the JS script on new_page.html?

header("Location: new_page.html");
$data = json_encode(response);
echo $data
4

1 回答 1

2

所以事实证明你不能使用 AJAX 进行文件上传。

不是真的

我可以使用重定向浏览器从我的 PHP 加载另一个页面

HTTP标Location头仅接受绝对URI。大多数浏览器会默默地从该错误中恢复,但您不应该以此为理由。

在此之后,如何将 JSON 响应发送到 new_page.html 上的 JavaScript 脚本。

你没有。您以编程方式生成该页面,并将其构建为如果您在页面加载后运行它,JavaScript 会将其放入的状态。

加载一个立即使用 JSON 获取数据以更新自身的页面只是偷工减料。您最终会遇到曾经困扰 Twitter 的默认内容闪现问题(在他们开始在初始加载时构建页面服务器端并仅在视图更改时使用 Ajax 更新它们之前)。

于 2013-07-10T19:03:24.757 回答