jQuery ajax调用不会触发Safari 9.1上的成功函数

时间:2016-05-27 10:13:05

标签: javascript jquery ajax

在一个项目中,我有一个特定的ajax调用,可以在PC Chrome / Firefox上正常工作,但在Safari 9.1上,它无法触发成功功能。

ajax电话:

$('#new_file_form').submit(function(e) {
            e.preventDefault();
            var form_data = new FormData($(this)[0]);

            $.ajax({
                url: '/includes/ajax/file-manager.php',
                type: 'POST',
                data: form_data,
                processData: false,
                contentType: false,
                success: function(result) {
                    JSON.parse(result);
                    alert(result);
                    if (result == true) {
                        $('#new_file_form').reset;
                        $('#new_file_modal').modal('hide');
                        bootbox.alert({
                            size: 'small',
                            message: '<i class="glyphicon glyphicon-info-sign blue"></i>File successfully saved.',
                            callback: function() {
                                location.reload();
                            }
                        });
                    } else if (result == false) {
                        bootbox.alert({
                            size: 'small',
                            message: '<i class="glyphicon glyphicon-exclamation-sign orange"></i>File not saved.',
                            callback: function() {
                                location.reload();
                            }
                        });
                    } else {
                        bootbox.alert({
                            size: 'small',
                            message: '<i class="glyphicon glyphicon-exclamation-sign orange"></i>Whoa! That escalated quickly..',
                            callback: function() {
                                location.reload();
                            }
                        });
                    }
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
        });

这不会引发任何错误。上传文件并将数据插入到数据库中,它就是那里的成功函数,似乎在嘲笑我。

根据Safari中返回的错误,发布file-manager.php代码似乎很有用:

if(!empty($_POST['file_name']) && !empty($_POST['file_type']) && !empty($_POST['file_user'])) { // && !empty($_FILES['file'])
    $ownerid = $mysqli->real_escape_string($_POST['file_user']);
    $ownertype = 1;
    $name = $mysqli->real_escape_string($_POST['file_name']);
    $visible = $mysqli->real_escape_string($_POST['visible']);
    $filetype = $mysqli->real_escape_string($_POST['file_type']);

    $ownerid = filter_var($ownerid, FILTER_SANITIZE_NUMBER_INT);
    $visible = filter_var($visible, FILTER_SANITIZE_NUMBER_INT);
    $filetype = filter_var($filetype, FILTER_SANITIZE_NUMBER_INT);

    $cdate = date('Y-m-d H:i:s');
    $edate = $cdate;

    $u_file_name = $_FILES['file']['name'];
    $u_tmp_file = $_FILES['file']['tmp_name'];
    $u_file_type = $_FILES['file']['type'];
    $u_file_error = $_FILES['file']['error'];
    $u_file_content = file_get_contents($_FILES['file']['tmp_name']);

    $upload_result = '';

    if($u_file_error == 'UPLOAD_ERROR_OK') {
        if($u_file_name == '') {
            $upload_result = false;
        } else {
            $sql = "SELECT path FROM filetypes WHERE id = '$filetype'";
            $result = $mysqli->query($sql);
            $record = $result->fetch_object();
            $file_type_path = $record->path;

            $extension = end(explode(".", $u_file_name));
            if($ownertype == 1) {
                $s_file_name = preg_replace("![^a-z0-9]+!i", "-", $name).'-'.date('Y-m-d_H-i').'.'.$extension;
                $s_file_dest = LOCAL_BASE_PATH.'/uploads/'.$ownerid.'/'.$file_type_path;
                $s_file_location = $s_file_dest.'/'.$s_file_name;
                $s_file_path = '/uploads/'.$ownerid.'/'.$file_type_path.$s_file_name;
            }
            if(!file_exists($s_file_dest)) {
                mkdir($s_file_dest, 0755, true);
            }
            move_uploaded_file($u_tmp_file, $s_file_location);

            $sql = "INSERT INTO files (ownertype, owner, type, visible, extension, name, path, cdate, edate) VALUES ('$ownertype', '$ownerid', '$filetype', '$visible', '$extension', '$s_file_name', '$s_file_path', '$cdate', '$edate')";
            $result = $mysqli->query($sql);

            $upload_result = true;
        }
    }

    echo json_encode($upload_result);
    exit();
}

当我在解析之前提醒ajax结果时,我收到一个PHP错误: 严格标准:只应通过引用传递变量...

0 个答案:

没有答案