douxi8759 2017-08-03 07:15
浏览 18
已采纳

我的Ajax正在为php发送两次帖子

I keep getting two posts on my table when I send my create.php for a smooth upload.

$(document).ready(function () {

/* Data Insert Starts Here */
$(document).submit('submit', '#SavePost', function () {

    $.post("create.php", $(this).serialize())
        .done(function (data) {
            $("#dis").fadeOut();
            $("#dis").fadeIn('slow', function () {
                $("#dis").html('<div class="alert alert-info">' + data + '</div>');
                $("#SavePost")[0].reset();
                $("body").fadeOut('slow', function () {
                    $("body").fadeOut('slow');
                    window.location.href = "index.php";
                });
            });
        });

    /* Image upload Ajax */
    $.ajax({
        url: "create.php",
        type: "POST",
        data: new FormData(this),
        contentType: false,
        cache: false,
        processData: false,
        beforeSend: function () {
            //$("#preview").fadeOut();
            $("#err").fadeOut();
        },
        success: function (data) {
            if (data == 'invalid file') {
                // invalid file format.
                $("#err").html("Invalid File !").fadeIn();
            } else {
                // view uploaded file.
                $("#preview").html(data).fadeIn();
                $("#SavePost")[0].reset();
            }
        },
        error: function (e) {
            $("#err").html(e).fadeIn();
        }
    });
    /* Image upload Ajax ENDING */
    return false;
});
/* Data Insert Ends Here */

I get one post with the image uploaded and one without. I tried to check if my post get sent twice and i have tried to delete a few lines but i can't seem to figure this one out.

Thanks!

  • 写回答

2条回答 默认 最新

  • dongmou3615 2017-08-03 07:25
    关注

    $.post and $.ajax are two seperate request, so you are sending two requests, the first one via the $.post method and right after that you use $.ajax to send another request. remove one of them, and you will receive only one request as you expected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog