duanduanxi9441 2016-05-07 03:08
浏览 34

为什么从PHP传递javascript函数有时会执行,有时不执行?

I am uploading a file to PHP and want to return errors in an alert or better yet in a div element. It seems that when I do not use alert, the function always fails to produce the desired result. Although pressing ctrl-F5 (chrome) to bypass caching sometimes works, but sometimes the following console message is shown:

writers.php:1 Uncaught ReferenceError: phpReturn is not defined

JS call to PHP

$('#writers').on('submit', function () {
    if (beforeSubmit()) {
        $(this).ajaxSubmit(options);
    } else return false;
}); 

This works just fine.

RelevantPHP error handler

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime_type = finfo_file($finfo, $UploadDirectory . $NewFileName);
    finfo_close($finfo);
    if ($mime_type != 'application/pdf') {
        unlink($UploadDirectory . $NewFileName);
        echo "<script type='text/javascript'>phpReturn('File MUST be a PDF!');</script>";
    }

JS function called from PHP

function phpReturn(errmsg) {
    if (errmsg != undefined) {
        alert(errmsg);
        $("#errmsg").text(errmsg).css({ 'display': 'block' });
    }
    window.location.href = "http://writers-tryst.ron-tornambe.com/#/writers";    

It is a little tricky to test this behavior. You need to rename a non-PDF file to one with a PDF extension. Here is the test site.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?