如何在浏览器控制台中记录使用jQuery AJAX运行的PHP文件? [英] How to log PHP file run with jQuery AJAX in browser console?

查看:42
本文介绍了如何在浏览器控制台中记录使用jQuery AJAX运行的PHP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个进行数据处理的PHP文件.它由 $.ajax()运行,有时在需要较长时间处理的大文件上运行.我需要在浏览器控制台中记录一些有关正在进行的进程的信息,这些信息在运行中显示为 ,而不仅仅是在PHP文件运行完毕时.

I have a PHP file that does data processing. It's run by $.ajax(), sometimes on big files that take a long time to process. I need to log some info about the ongoing process in the browser console that is displayed on the go, not just when the PHP file has finished running.

从我所做的研究中,我发现有两个困难:

From the research I did, I get that there are two difficulties:

  1. 让PHP在完成之前吐出一些东西
  2. 获取jQuery/JS以便随时随地显示它

为解决#1问题,我已经尝试过:

To address #1, I've tried:

echo "started"."<br>";

foreach (array("done this", "done that","had a coffee","burp") as $msg) {
    sleep(3);
    echo $msg."<br>";
    flush();
    ob_flush();
}

flush();尽管可以测试

flush(); ob_flush(); is supposed to do the job, although as you can test here it does not strictly display ever 3s as it's expected to. Any suggestion to get it to display as expected?

关于如何解决#2的问题,我已经探索了一个涉及 XMLHttpRequest 的解决方案,但是我对这个主题并不熟悉,因此既不确定要寻找的内容还是正确的方向...

As for how to address #2, I have explored a solution involving XMLHttpRequest, but I'm not familiar with the subject so not sure neither what to look for nor if it's the right direction...

这是我要运行的测试代码:

Here is the test code of what I'm trying to get to run:

$("#run").click(function(e) {
  $.ajax({
    url: "http://constances-web-dev.vjf.inserm.fr/constances-web/ajax-test.php",
    xhr: function() {
      // get the native XmlHttpRequest object
      var xhr = $.ajaxSettings.xhr();
      xhr.addEventListener('readystatechange', function(e) {
        console.log(e)
      });
      // set the onprogress event handler
      //xhr.onprogress = function(evt){ console.log(evt.target.response) } ;
      // set the onload event handler
      return xhr;
    },
    success: function(msg) {
      console.log(msg);
    },
    error: function(msg) {
      console.log("Erreur: " + msg);
    }
  })
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="run">go</button>

子问题:是否有一种(简便的)方法可以绕过跨域限制,以使AJAX示例可在SO代码段中使用?

Subsidiary question: Is there an (easy) way to go around the cross-origin restriction in order to get an AJAX example to work in a SO snippet?

推荐答案

作为一种解决方法,我已将日志文件添加到我的write2log函数中

as a work around I've added a log to file to my write2log function

static function log2File($string, $logFileName) {
        if (substr($string,0,1 ) == "\n")
            exec("echo '".date('Y-m-d_H:i:s')." ".addslashes(substr($string,1))."' >> ".$logFileName,$output,$status);
        else
            exec("echo -n ".addslashes($string)."' >> ".$logFileName,$output,$status);
    }

然后,我可以 tail -f 日志以查看事情的进展

I can then tail -f the log to watch how things are going

但是这需要通过ssh访问服务器,因此我仍然想弄清楚如何登录控制台

but this takes ssh access to the server so i'd still be interested in figuring out how to log to console

这篇关于如何在浏览器控制台中记录使用jQuery AJAX运行的PHP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆