简体   繁体   English

Ajax multipart / formdata发布请求

[英]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. 我正在尝试通过AJAX向第三方API发送发布请求,但我被退回了这两个我无法超越或修复的错误。

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. 不赞成在主线程上使用同步XMLHttpRequest,因为它会对最终用户的体验产生不利影响。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://my-provided-api-url 跨域请求被阻止:同源策略禁止在https:// my-provided-api-url读取远程资源

Here is my HTML submit form: 这是我的HTML提交表单:

<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: 这是我的AJAX请求的jQuery代码:

$(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. 错误的解释是,当您在主线程上执行Synchronous XMLHttpRequest时,在未收到响应的情况下,用户操作被阻止。 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 您可以在此处看到不同之处同步和异步ajax请求之间的区别

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 不赞成在主线程上使用同步XMLHttpRequest,因为它对最终用户的体验有不利影响

This is because you're using async: false , which is incredibly bad practice. 这是因为您使用的是async: false ,这是非常糟糕的做法。 It blocks the UI thread from updating while the request is running, which looks to the user like the browser has hung. 它在请求运行时阻止UI线程更新,这对用户来说就像浏览器已挂起一样。 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 跨域请求被阻止:同源策略禁止在https:// my-provided-api-url读取远程资源

This is a much larger problem. 这是一个更大的问题。 The domain you're making the request to does not have CORS enabled. 您向其发送请求的域未启用CORS。 This means that you cannot make a request to their API through JavaScript as you will be stopped buy the Same Origin Policy . 这意味着您无法通过JavaScript向其API请求,因为您将停止购买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. 作为一种解决方法,您可以向服务器发出本地AJAX请求,然后将请求代理给第三方,然后将数据返回给您。

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

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