求助django ajax 跨域请求失败的解决方法
1个回答
展开全部
<!DOCTYPE HTML PUBLIC "-
<html>
<head>
<title>Register form</title>
<script src=" " type="text/javascript"></script>
<script type="text/javascript">
function getZipData() {
var zipcode = $('#zip').val();
$.get('/' + zipcode + '/', function(data) {
$('#city').attr({'value':data});
});
}
</script>
</head>
<body>
<table>
<form action="" method="post">
<tr><td>Zip:</td><td><input id="zip" onblur="getZipData()" type="text" name="zip"/></tr>
<tr><td>City:</td><td><input id="city" type="text" name="city"/></tr>
</form>
</table>
</body>
</html>
view.py:
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from django.core import serializers
from django.shortcuts import render_to_response
def getzipdata(request, zipcode):
if zipcode == '214028':
city = 'bijing'
else:
return HttpResponse(city)
def register(request):
return render_to_response('form.html', {})
#响应头加上跨域允许
response = HttpResponse()
response['Access-Control-Allow-Origin'] = '*'
return response
#跑公网容易被攻击
转载,仅供参考。
<html>
<head>
<title>Register form</title>
<script src=" " type="text/javascript"></script>
<script type="text/javascript">
function getZipData() {
var zipcode = $('#zip').val();
$.get('/' + zipcode + '/', function(data) {
$('#city').attr({'value':data});
});
}
</script>
</head>
<body>
<table>
<form action="" method="post">
<tr><td>Zip:</td><td><input id="zip" onblur="getZipData()" type="text" name="zip"/></tr>
<tr><td>City:</td><td><input id="city" type="text" name="city"/></tr>
</form>
</table>
</body>
</html>
view.py:
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from django.core import serializers
from django.shortcuts import render_to_response
def getzipdata(request, zipcode):
if zipcode == '214028':
city = 'bijing'
else:
return HttpResponse(city)
def register(request):
return render_to_response('form.html', {})
#响应头加上跨域允许
response = HttpResponse()
response['Access-Control-Allow-Origin'] = '*'
return response
#跑公网容易被攻击
转载,仅供参考。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询