【问题标题】:Upload file via ajax jquery php api通过ajax jquery php api上传文件
【发布时间】:2013-08-28 09:44:08
【问题描述】:

我有以下格式的代码。

PHP 文件:

<form action="http://clientwebapi.com/createEvent" id="form_createEvent" method="post" enctype="multipart/form-data">
<input type="text" name="image_title" />
<input type="file" name="media" accept="image/*,video/*"/>
</form>

JQUERY 文件:

$('#form_createEvent').submit(function () {
    var form = $(this);
    $.ajax({
        url: form.attr("action"),
        type: form.attr("method"),
        xhrFields: {
            withCredentials: true
        },
        data: form.serialize()
    }).done(function () {
        showCurrentLocation();
        alert('Event created successfully..');
        location.reload();

    }).fail(function () {
        alert("fail!");
    });
    event.preventDefault();
});

以上 Jquery 代码正在提交中。此外,当我提交以下格式时,它将重定向到“http://clientwebapi.com/createEvent”并成功创建事件。

表单提交并重定向到客户端页面:

$('#form_createEvent').submit(function () {
    var fd = new FormData();
    fd.append('media', input.files[0]);

    $.ajax({
        url: form.attr("action"),
        data: fd,
        processData: false,
        contentType: false,
        type: form.attr("method"),
        success: function (data) {
            alert(data);
        }
    });
event.preventDefault();

});

在此处提交表单并使用给定图像创建事件时如何防止。请帮忙

【问题讨论】:

  • $.ajax()之后最后做return false;
  • 或者right way
  • 实际上我正在将包括图像在内的数据发送到“clientwebapi.com/createEvent”。处理完之后,它会发送一些响应。我需要知道我们如何通过这个 jquery、ajax 代码将图像传递给客户端 API

标签: php jquery ajax multipartform-data


【解决方案1】:

您忘记将 event 作为参数添加到 submit 函数:

$('#form_createEvent').submit(function (event) {

【讨论】:

  • 那是因为你的 ajax 代码后面有event.preventDefault();
【解决方案2】:

我找到了答案。我在这里犯了一些错误。我使用下面的代码解决了..

$('#form_createEvent').submit(function() { 
            var form = new FormData($(this)[0]); 
            $.ajax({
                url: 'http://clientwebapi.com/createEvent/',
                type: 'POST',
                xhrFields: {
                    withCredentials: true
                },
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                data: form,
                beforeSend: function () {
                    $("#output_process").html("Uploading, please wait....");
                },
                success: function () { 
                    $("#output_process").html("Upload success.");
                },
                complete: function () {
                    $("#output_process").html("upload complete.");
                },
                error: function () {
                    //alert("ERROR in upload");
                    location.reload();
                }
            }).done(function() { 
                alert('Event created successfully..');

            }).fail(function() {
                alert("fail!");
            });
            event.preventDefault();
        });

【讨论】:

    【解决方案3】:

    您可以通过return false;停止表单提交

    $('#form_createEvent').submit(function () {
        // some code
        $.ajax({
            // some code/objects
        });
    
        return false; // here, it will stop the form to be post to the url/action
    
    });
    

    【讨论】:

    猜你喜欢
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 2010-10-18
    • 2017-08-25
    • 2014-05-07
    相关资源
    最近更新 更多