我在实现自我替换脚本时遇到了麻烦。我承认我非常困惑。
我正在尝试实现两个按钮,这样如果您单击一个按钮,则会将 POST 请求发送到服务器;服务器验证您确实访问了该网页并将 HttpResponseRedirect 发送回包含不完整网页的页面/单击按钮后仅包含我想要更新的部分的页面。一旦我的原始 jQuery 请求收到该新页面,它将用刚刚收到的新页面替换当前页面上的元素。
我的 jQuery 看起来像:
<script id="self_replacing">
$(document).ready(function() {
$('#button1').click(function(){
$.post("/", {
unique_id : "{{ unique_id }}",
csrfmiddlewaretoken: '{{ csrf_token }}'
},
function(data){
var js_portion = $('<div/>').html(data).find('self_replacing')
var html_portion = $('<div/>').html(data).find('pic_content')
$('#pic_content').replaceWith(html_portion)
$('#self_replacing').replaceWith(js_portion)
});
});
});
</script>
不完整的页面:
<script id="self_replacing">
$(document).ready(function() {
$('#button1').click(function(){
$.post("/", {
unique_id : "{{ unique_id }}",
csrfmiddlewaretoken: '{{ csrf_token }}'
},
function(data){
var js_portion = $('<div/>').html(data).find('self_replacing')
var html_portion = $('<div/>').html(data).find('pic_content')
$('#pic_content').replaceWith(html_portion)
$('#self_replacing').replaceWith(js_portion)
});
});
});
</script>
<div id="pic_content">
.
.
</div>
我有正确的想法吗?当我通过 FireBug 控制台查看我的请求时,每次单击按钮时流量都会开始向上扩展。
例如,在开始时,1 点击 = 1 POST,然后 1 点击 = 2POST,然后 1 点击 = 3 POST。我不太确定为什么会这样。我的 jQuery 脚本有问题吗?
注意:jQuery 脚本也需要在每次 POST 后更新。