简体   繁体   中英

Ajax multipart/formdata post request

I'm trying to send a post request to a third party API via AJAX but I'm being returned these two errors which I cannot surpass or fix.

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://my-provided-api-url

Here is my HTML submit form:

<div class="upload">
    <h2>Select a file for upload</h2>
    <form name="addProductForm" id="addProductForm" action="javascript:;" enctype="multipart/form-data" method="post" accept-charset="utf-8">
        <input type="file" id="myFile">
        <input type="submit" value="Submit" id ="submit-button">
    </form> 
</div>

Here is my jQuery code for the AJAX request:

$(document).ready(function () {
    $("#addProductForm").submit(function (event) {
        event.preventDefault();  
        var formData = $(this).serialize();

        $.ajax({
            url: 'https://my-provided-api-url',
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function () {
                alert('Form Submitted!');
            },
            error: function(){
                alert("error in ajax form submission");
            }
        });
        return false;
    });
});

Thank you in advance.

The error cause is the definition of the request, exactly this line:

async: false

The explanation of the error is that when you do a Synchronous XMLHttpRequest on the main thread user actions are blocked while response has not been received. You can do it but is deprecated

You can do a synchronous call:

async: false

You must be careful because the behavior is different. You can see diferents here Diferences between sync and async ajax request

Dealing with the two errors in turn:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience

This is because you're using async: false , which is incredibly bad practice. It blocks the UI thread from updating while the request is running, which looks to the user like the browser has hung. Always make your requests asynchronous and use callbacks to handle the response.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://my-provided-api-url

This is a much larger problem. The domain you're making the request to does not have CORS enabled. This means that you cannot make a request to their API through JavaScript as you will be stopped buy the Same Origin Policy . As a workaround you could make a local AJAX request to your server which then proxies the request to the third party and returns you back the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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