脚本宝典收集整理的这篇文章主要介绍了Django【view】cors跨域与ajax get请求,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
pip install django-cors-headers
INSTALLED_APPS = [
...
'corsheaders',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.securITy.SecurityMiddleware',
...
]
# cors
CORS_ALLOW_CredENTIALS = True
CORS_ORIgin_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = ()
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'accept',
'accept-encoding',
'authorization',
'content-tyPE',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
)
<script src="https://code.jquery.COM/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script type="text/javascript">
function getGoods(){
VAR barcode = $("#barcode").val();
$.ajax({
type: "GET",
url: "http://127.0.0.1:8000/get_goods/?barcode="+barcode,
headers: {
'Content-Type': 'applocation/json'
},
cache: false,
success: function (response) {
if (response.status === 200) {
alert(response.result);
console.LOG(response.result)
} else {
alert(response.result);
}
}
})
}
</script>
以上是脚本宝典为你收集整理的Django【view】cors跨域与ajax get请求全部内容,希望文章能够帮你解决Django【view】cors跨域与ajax get请求所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。