【发布时间】:2023-10-07 08:32:01
【问题描述】:
想用csrf_token解决ajax的跨域问题,但是遇到了一些问题
这是我的 json:
{
"id": 1,
"distribution_box_id": "abc123",
"timestamp": "2019-07-11T07:00:00",
"lock1": 0,
"lock2": 0,
"temperature": 26.0,
"humidity": 10.0,
"latitude": 35.15409152643205,
"longitude": 109.45553839575084,
"smoke_detector": 0
}
<script>
$.ajax({
url: 'http://localhost/app1/distributionboxdata/1',
method: 'post',
headers: {
'Csrf-Token': '@play.filters.csrf.CSRF.getToken.map(_.value)'
},
data: {
name: '@name'
},
success: function (data, textStatus, jqXHR) {
location.reload();
},//I don't know what to put here
error: function (jqXHR, textStatus, errorThrown) {
debugger;
}
});
</script>
【问题讨论】:
-
您能否使您的代码清晰易读……您的问题是什么?控制台出错?
-
控制台错误是:访问 XMLHttpRequest at 'localhost/app1/distributionboxdata/1' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested资源。
-
我尝试使用csrf_token解决ajax的跨域问题
-
看起来你只需要修复服务器上的 CORS 标头。
-
如果您的网页是使用
file:///协议加载的,那么您可能无法轻松修复CORS
标签: javascript html django ajax