php会话上传进度堆栈与跨域

The session upload progress work fine but when i change the from post url from pros.php to server_ip/pros.php and change XMLHttpRequest JavaScript get url from

t.open("GET","progress.php",false);

to

t.open("GET","server_ip/progress.php",false);

it is not work so i add in progress.php header("Access-Control-Allow-Origin: *"); and it still not working

progress.php:

  <?php 
error_reporting(0); 
header("Access-Control-Allow-Origin: *"); 
session_start(); 
$key = ini_get("session.upload_progress.prefix") . 'progressperc';
if(isset($_SESSION[$key]['bytes_processed']) 
   AND $_SESSION[$key]['bytes_processed'] != 0) { 
   $file_uploaded = true; 
   echo round($_SESSION[$key]['bytes_processed'] / $_SESSION[$key]['content_length'] * 100);
} elseif ($file_uploaded === true 
          AND $_SESSION[$key]['bytes_processed'] == 0) {
  echo 100; 
} 
?>

and this is js code in index.php to get progress

<script>window.setInterval(function(){var t;if(window.XMLHttpRequest){t=new XMLHttpRequest}else{t=new ActiveXObject("Microsoft.XMLHTTP")}t.onreadystatechange=function(){if(t.readyState==4&&t.status==200){if(t.responseText=="100"){document.getElementById("progress").style.width=t.responseText+"%"document.getElementById("progress").innerHTML=t.responseText+"%";clearInterval()}if(t.responseText!=""&&t.responseText!="100"){document.getElementById("progress").style.width=t.responseText+"%";document.getElementById("progress").innerHTML=t.responseText+"%"}}};t.open("GET","progress.php",false);t.send(null)},5e3)</script>

Access-Control-Allow-Origin is different between broswers. Most of time it is only used when submit a form from one domain to another.

By default, cross-domain http request is forbidden, but you can do cross-domain json request. You can make progress.php return in json format with header:

content-type:application/json.

If you just want to show an upload progress value, you can also try to use a JS hack (by add a new tag to html, the script will run), or make a progress image (update the img src). Both 100% working on all broswers.