【发布时间】:2013-08-13 21:33:23
【问题描述】:
javascript:
$('.openDiv').click(function () {
var id = $(this).attr('id');
var csrf_token = $("#csrf_token").val();
$.ajax({
data: {
csrfmiddlewaretoken: ('{{csrf_token}}'),
id: id,
},
type: 'POST',
url: '/setting/save-reporter/',
success: function () {
$('#authorisedreporter').load('save_reporter.html');
}
});
});
index.html
<div id="authorisedreporter" {% if not registerform.errors %}style="display:none"{% endif %}>
<form method="post" action="." id="reporter-form">
{% csrf_token %}
<table width="100%">
""""""""
</table></form></div>
save_reporter.html
<form method="post" action="." id="{{ id }}">
{% csrf_token %}
<table width="100%">
"""""""""
</table></form>
我正在使用 ajax 和 js,在成功调用 ajax 后,div 被 save_reporter.html 替换。但我在控制台中收到 404 page not found 错误。如果我没有使用$('#authorisedreporter').load('save_reporter.html');,我得到了所需控制台中的输出。尝试在模板中显示输出时,我这样做了,但出现错误。需要帮助。
【问题讨论】:
-
action="."不按照你的想法做,让它为空。 404 很可能是因为"/setting/save-reporter/"不是原始 AJAX 帖子的有效 URL。 -
@AdamKG 以前我在控制台中获取输出,在 js 中进行此更改后 $('#authorisedreporter').load('save_reporter.html');出现 404 错误。
-
@AdamKG 我不知道你认为 OP 认为
action="."做了什么,或者你认为它做了什么。然而,这样做是正确的事情,而action=""会做错误的事情,例如如果原始URL没有以最后的斜杠结尾。 -
@DanielRoseman 就是这样,不知道为什么我会这么想,谢谢和 TIL!
标签: javascript python ajax django django-models