Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 防止ajax进度代码过早中止_Javascript_Ajax_Html - Fatal编程技术网

Javascript 防止ajax进度代码过早中止

Javascript 防止ajax进度代码过早中止,javascript,ajax,html,Javascript,Ajax,Html,我正在使用index.php <script type="text/javascript"> $(function(){ $("#progressbar").progressbar({ value: 0 }); function load() { $.ajax({ url: './ajax.php?status='+$( "#progressba

我正在使用index.php

<script type="text/javascript">
    $(function(){
        $("#progressbar").progressbar({
            value: 0 
        });

        function load() {
            $.ajax({
                url: './ajax.php?status='+$( "#progressbar" ).progressbar( "value" ),
                success: function(data) {

                    ajax = eval('(' + data + ')');

                    if(ajax!=false) {
                        $("#progressbar").progressbar({
                            value: ajax.status
                        });

                        $("#message").html( ajax.message );

                        if(ajax.status!=100) {
                            load();
                        }
                    }
                }
            });
        }

        load();
    });
</script>

<div id="message"></div>
它被转换为整数,并通过json转发到index.php

这有时有效。这意味着,在index.php上,有时我看到了进展,有时我看不到


如何使index.php中的代码等待ajax.php响应,而不是(可能)过早中止?

请使用
$.getJSON
eval()
只是在问问题。我在使用JSON2或JSON3时读到“并非所有浏览器都支持原生JSON,因此有时需要对JSON字符串使用eval()”。比eval快得多。@PeterWeter:不再是了(反正你也不想支持这样的旧浏览器)。如果必须的话,您仍然可以使用
$.parseJSON
,但是使用正确的MIME类型,jQuery的ajax函数已经为您做到了这一点。@Bergi使用您的方法,这段代码看起来像什么?
<?php

    $php_array['status'] = rand(0,99);

  if($php_array['status']>100) {
      $php_array['status'] = 100;
  }

  if($php_array['status'] != 100) {
      $php_array['message'] = 'Aktueller Status <b>'.$php_array['status'].'%</b> von 100%, Differenz: '.(100-$php_array['status']);
  } else {
      $php_array['message'] = 'Juhu endlich geschafft!';
  }


  // Ausgabe des PHP Arrays als JSON Objekt
  echo json_encode($php_array);
?>
<?php

  //modified - start
  $zahl = 0;
  $menge = 0;

  if ($handle = opendir('folder')) 
  { 
    while (false !== ($entry = readdir($handle))) 
    {
        if (strpos($entry,$_GET['session_id']) !== false) 
        {                                        
            if (strpos($entry,'result') !== false)
            {
            }
            else
            {
                $zahl += (int)file_get_contents('folder/'.$entry);
                $menge++;        
            } 
        }
    }
    closedir($handle);
  }

  if((int)($zahl/$menge) != 0.0)
  { 
    $php_array['status'] = (int)($zahl/$menge);
  }
  else
  {
    $php_array['status'] = 0;
  }

  //modified - end

  if($php_array['status']>100) {
      $php_array['status'] = 100;
  }

  if($php_array['status'] != 100) {
      $php_array['message'] = 'Aktueller Status <b>'.$php_array['status'].'%</b> von 100%, Differenz: '.(100-$php_array['status']);
  } else {
      $php_array['message'] = 'Juhu endlich geschafft! <img src="http://d4nza.de/blog/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley"> ';
  }
  echo json_encode($php_array);
?>
(int)($zahl/$menge)