【问题标题】:jQuery AJAX File Upload FreezesjQuery AJAX 文件上传冻结
【发布时间】:2014-12-01 00:25:09
【问题描述】:

我正在使用 jQuery 的 ajax 函数上传文件。文件上传工作完美,数据发送正确,但 HTML 没有更新。提交表单后,按钮不会重新启用,并且控制台中会显示错误:“Uncaught SyntaxError: Unexpected Identifier”。错误的行号是我的 HTML 文档的第一行。

这是我的 JavaScript 代码:

jQuery(function($) {
    $('form[data-async]').on('submit', function(e) {
        var $form = $(this);
        var $target = $($form.attr('data-target'));
        $form.find('button').attr('disabled', 'disabled');
        $.ajax({
            url: $form.attr('action'),
            type: $form.attr('method'),
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function(data, status) {
                $target.html(data.response).css('display', 'block');
                $form.find('button').removeAttr('disabled');
            },
            error: function(data, status) {
                $target.html(data.response).css('display', 'block');
                $form.find('button').removeAttr('disabled');
            }
        });
        e.preventDefault();
    });
});

来自服务器的 JSON 响应也不会显示。这是我的 PHP 代码的响应:

return Response::json(['response' => 'Image has been uploaded! <script>window.location.href="/image/"' . $filename . '"</script>']);

什么可能导致错误被抛出并且没有显示响应?

【问题讨论】:

    标签: javascript php jquery html ajax


    【解决方案1】:

    我会查看 jquery 的行,您似乎已经开始重新编写 PHP 代码:

    $target
    $form 
    

    SB $.target 或 $('#target') 或类似的,不是吗?

    【讨论】:

      【解决方案2】:

      解决了问题。

      return Response::json(['response' => 'Image has been uploaded! <script>window.location.href="/image/"' . $filename . '"</script>']);
      

      return Response::json(['response' => 'Image has been uploaded! <script>window.location.href="/image/' . $filename . '"</script>']);
      

      【讨论】:

        猜你喜欢
        • 2018-06-19
        • 2021-12-15
        相关资源
        最近更新 更多