weixin_33725807 2020-03-03 04:57 采纳率: 0%
浏览 44

禁用表格重定向

So I have this form, that uploads a file to a third party host, upload works fine, but I'm getting a redirect when upload is done, I have read that Ajax can solve this, but I have tried many Ajax solutions and non seem to have worked, might it be because the action is done outside of my website? any help is much appreciated!

<form id="myform" enctype="multipart/form-data" action="https://website/upload/01" method="post">
    <input type="hidden" name="api_key" value="XXXXXXX">
    <input name="file" type="file">
         <input value="Upload" type="submit">    

  • 写回答

1条回答 默认 最新

  • weixin_33725722 2020-03-03 05:07
    关注

    You can use e.preventDefault();

    Here i attached sample code with Jquery and ajax.

    jQuery + Ajax

     $("form#myform").submit(function(e) {
            e.preventDefault();    
            var formData = new FormData(this);
    
            $.ajax({
                url: window.location.pathname, // location of form submit
                type: 'POST',
                data: formData,
                success: function (data) {
                    alert(data)
                },
                cache: false,
                contentType: false,
                processData: false
            });
        });
    

    Reference: Uploading both data and files in one form using Ajax?

    评论

报告相同问题?