我想在前端使用 Javascript、jQuery、AJAX 调用 Django Rest API。请求方法是 POST 但当我看到 API 调用它的调用 OPTIONS 方法时。所以,我开始知道access-control-allow-origin
我猜需要在 API 中允许哪些。为此,我使用了django-CORS-headers
包,但仍然调用了该OPTIONS
方法。
代码是这样的:
jQuery.ajax({
url: API_url,
headers:headers,
dataType: "JSON",
type: "POST",
crossDomain: true,
xhrFields: {
withCredentials: true
},
success: function( response, jqXHR ) {
do something here
}
});
Run Code Online (Sandbox Code Playgroud)