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 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 matlab求解平差
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办