반응형
Javascript로 로드된 iframe에서 HTTP 상태 코드 검색
비동기 양식 제출에 jQuery Form 플러그인을 사용했습니다.파일이 포함된 양식의 경우 양식을 숨겨진 iframe에 복사하고 제출하고 iframe의 내용을 다시 복사합니다.문제는 서버에서 반환된 HTTP 상태 코드를 찾는 방법을 알 수 없다는 것입니다.예를 들어 서버가 404를 반환하면 iframe의 데이터는 정상으로 복사되어 일반 응답으로 취급됩니다.
이프람 물체 안을 뒤져보기도 했는데status_code
아직 그런 걸 못 찾았어요
그$.ajax()
파일 업로드를 지원하지 않기 때문에 사용할 수 없습니다.비동기식으로 파일을 업로드하는 유일한 방법은 숨김을 사용하는 것입니다.iframe
방법.
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>
서버 측:
/*
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>';
?>
로드된 "iframe"에서 HTTP 상태 코드를 직접 검색할 수 없습니다.그러나 http 오류가 발생하면 서버는 "iframe"에 아무것도 반환하지 않습니다.iframe에 내용이 없습니다.iframe 본문을 체크할 수 있습니다.iframe 본문이 공백일 경우 동일한 URL을 가진 ajax를 사용하여 서버에서 응답을 가져옵니다.그런 다음 응답에서 http 상태 코드를 가져올 수 있습니다.
언급URL : https://stackoverflow.com/questions/35240/retrieving-http-status-code-from-loaded-iframe-with-javascript
반응형
'programing' 카테고리의 다른 글
Woocommerce 카트에서 각 제품의 작성자 ID를 가져옵니다. (0) | 2023.03.02 |
---|---|
ajax 함수로 호출된 파일에 대한 직접 액세스 방지 (0) | 2023.03.02 |
Wordpress 호스트 IP가 변경되었습니다. (0) | 2023.03.02 |
AngularJS의 다른 컨트롤러에서 함수를 호출하려면 어떻게 해야 합니까? (0) | 2023.02.25 |
조건부로 "ui-sref"를 실행하는 방법은 무엇입니까? (0) | 2023.02.25 |