jQuery 使用 Javascript 从加载的 iframe 中检索 HTTP 状态代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving HTTP status code from loaded iframe with Javascript
提问by John Millikin
I used the jQuery Form plugin for asynchronous form submission. For forms that contain files, it copies the form to a hidden iframe, submits it, and copies back the iframe's contents. The problem is that I can't figure out how to find what HTTP status code was returned by the server. For example, if the server returns 404, the data from the iframe will be copied as normal and treated as a regular response.
我使用 jQuery Form 插件进行异步表单提交。对于包含文件的表单,它将表单复制到隐藏的 iframe,提交它,然后复制回 iframe 的内容。问题是我不知道如何找到服务器返回的 HTTP 状态代码。例如,如果服务器返回 404,则来自 iframe 的数据将被正常复制并视为常规响应。
I've tried poking around in the iframe objects looking for some sort of status_code
attribute, but haven't been able to find anything like that.
我试过在 iframe 对象中寻找某种status_code
属性,但找不到类似的东西。
The $.ajax()
function can't be used, because it does not support uploading files. The only way to asynchronously upload files that I know of is using the hidden iframe
method.
该$.ajax()
功能无法使用,因为它不支持上传文件。我所知道的异步上传文件的唯一方法是使用 hiddeniframe
方法。
回答by Coyod
You can't get page headers by JS, but you can distinguish error from success: Try something like this:
你不能通过 JS 获取页眉,但是你可以区分错误和成功:尝试这样的事情:
<script type="text/javascript">
var uploadStarted = false;
function OnUploadStart(){
uploadStarted = true;
}
function OnUploadComplete(state,message){
if(state == 1)
alert("Success: "+message);
else
if(state == 0 && uploadStarted)
alert("Error:"+( message ? message : "unknow" ));
}
</script>
<iframe id="uploader" name="uploader" onload="OnUploadComplete(0)" style="width:0px;height:0px;border:none;"></iframe>
<form id="sender" action="/upload.php" method="post" target="uploader" enctype="multipart/form-data" onsubmit="OnUploadStart()">
<input type="file" name="files[upload]"/>
<input type="submit" value="Upload"/>
</form>
On server side:
在服务器端:
/*
file: upload.php
*/
<?php
// do some stuff with file
print '<script type="text/javascript">';
if(success)
print 'window.parent.OnUploadComplete(1,"File uploaded!");';
else
print 'window.parent.OnUploadComplete(0, "File too large!");';
print '</script>';
?>
回答by choi gray
You can't retrieving HTTP status code from loaded "iframe" directly. But when an http error occured, the server will returned nothing to the "iframe". So the iframe has not content. you can check the iframe body, when the body of iframe is blank, use ajax with the same url to get the response from server. Then you can retrieve the http status code from response.
您无法直接从加载的“iframe”中检索 HTTP 状态代码。但是当发生 http 错误时,服务器不会向“iframe”返回任何内容。所以 iframe 没有内容。您可以检查 iframe 正文,当 iframe 正文为空白时,使用具有相同 url 的 ajax 从服务器获取响应。然后您可以从响应中检索 http 状态代码。