简体   繁体   English

防止Ajax和PHP中的重定向页面

[英]Prevent redirect page in Ajax and PHP

I configure a file upload ability on my website. 我在网站上配置了文件上传功能。 The program work like a charm! 该程序像一个魅力! Though I use Ajax to handle the php response. 虽然我使用Ajax处理php响应。 The problem is that when I click submit it redirects me to php and don't return the echo back to my ajax. 问题是,当我单击“ submit它会将我重定向到php,并且不将echo返回给我的ajax。 I tried e.preventDefault() and return false to Ajax but dodn't work. 我尝试了e.preventDefault()并将return false给Ajax,但是不起作用。

Here are my files 这是我的档案

HTML HTML

<form style="margin-top: -20px; width: inherit; cursor: default;" name="photo" id="imageUploadForm" enctype="multipart/form-data"  method="post" action="../lib/imgUpload.php">
      <input type="file" name="image" id="ImageBrowse"  style='//visibility:hidden;'>
      <button type="submit" name="upload" id="UploadImageBtn" value="Upload">Upload</button>
</form>

JS JS

$(document).ready(function(e){
    //many many other functions

    $('#imageUploadForm').on('submit', function(e) {
        var formData = new FormData(this);

        $.ajax({
            type:'POST',
            url: $(this).attr('action'),
            data:formData,
            cache:false,
            contentType: false,
            processData: false,
            success:function(data){
                alert(data);

            },
            error: function(data){
                alert('bad');
            }
        });
        e.preventDefault();

    }));

    $("#ImageBrowse").on("change", function(e) {
        e.preventDefault();
        $("#imageUploadForm").submit();
    });
});

PHP PHP

<?php
    session_start();
    $sourcePath = $_FILES['image']['tmp_name'];      
    $targetPath = "profile_avatars/".$_FILES['image']['name'];
    $res = move_uploaded_file($sourcePath,$targetPath) ; 
    if ($res) {
        echo "yes";
    }
    else {
        echo "no";
    }
?> 

Try to execute e.preventDefault() before the other lines of code, like this: 尝试在其他代码行之前执行e.preventDefault(),如下所示:

$('#imageUploadForm').on('submit', function(e) {

    e.preventDefault();

    var formData = new FormData(this);

    $.ajax({
        method:'POST',
        url: "../lib/imgUpload.php",
        data:formData,
        cache:false,
        contentType: false,
        processData: false,
        success:function(data){
            alert(data);

        },
        error: function(data){
            alert('bad');
        }
    });
});

There also was one round closing bracket too much at the end of this function. 在此功能结束时,也有一个圆形的闭合括号。

Try to remove the action from your form element, and return false, like this: 尝试从表单元素中删除操作,然后返回false,如下所示:

 <form style="margin-top: -20px; width: inherit; cursor: default;" name="photo" id="imageUploadForm" enctype="multipart/form-data" onsubmit="return false;">

Remove method="POST" from the form element and add it to your ajax call. 从表单元素中删除method="POST"并将其添加到您的ajax调用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM