CORS错误与前端或后端有关吗?

时间:2018-10-11 11:12:29

标签: jquery

CORS错误与前端或后端有关吗?如果来自前端,该如何处理?我得到Error

我前进了一点。现在,我收到No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access错误。如何添加标题?

我的Ajax呼叫是:

$.ajax({
                    type: "POST",
                    url: url,
                    //contentType: "application/json; charset=utf-8",
                    crossDomain:true,

                    data: JSON.stringify(feederData),
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        if (data === true) {
                            alert();
                        }
                    },
                    error: function (a, b, c) {
                        console.log(a);
                        alert(a);
                    }
                });

2 个答案:

答案 0 :(得分:0)

Ajax端点不支持OPTIONS方法。

您需要在后端添加对OPTIONS的支持,并在OPTIONS上添加CORS

您可能已在客户端上添加了一个标头,该标头使AJAX触发器preflight

回答新问题

Basic AJAX request gets "No 'Access-Control-Allow-Origin' header is present on the requested resource" error

答案 1 :(得分:0)

这是一个后端问题。就是说,端点上不允许OPTIONS方法请求。 405 Method Not Allowed

使用CORS标头,但是端点需要允许OPTIONS请求。获取这些CORS标头的预检请求使用OPTIONS方法,服务器应以包含标头的空200响应进行响应。