由于HTML标签的iframe标签具有局部加载内容的特性,所以可以使用其来伪造Ajax请求。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body><input type="text" id='url'>
<input type="button" value="发送iframe请求">
<iframe id="ifm" src="http://www.baidu.com"></iframe><form action="/xiaoqing/ajax_json/" method="post" target="ifm1">
{% csrf_token %} <iframe id="ifm2" name="ifm1"></iframe>
<input type="text" name="username">
<input type="text" name="email">
<input type="submit" onclick="submitForm();" value="Form提交" ></form><script src="/static/jquery-1.12.4.js"></script>
<script> $(function () { $(':button').click( function () {
var url=$('#url').val();
$('#ifm').attr('src',url); }
) }) function submitForm() { $('#ifm2').load(function(){ var text= $('#ifm2').contents().find('body').text(); #这个地方要注意
var obj= JSON.parse(text);
console.log(obj);
}) }</script></body>
</html>
iframe.html
import json
def ajax_json(request): ret = {'status':True,'data':request.POST.get('username')} return HttpResponse(json.dumps(ret))def iframe(request):
return render(request,'iframe.html')
views.py