Javascript 检索新更新的文章而不刷新页面

Javascript 检索新更新的文章而不刷新页面,javascript,jquery,django,Javascript,Jquery,Django,我想创建一个编辑链接,在单击文章时更新文章, 在模板中,其结构如下: 后文 文章内容 文章形式 后期菜单 在单击编辑链接之前,我首先将“文章表单”隐藏为 <div class = "col-md-12 post-text" > <div class="article-content"> {{article.content}} </div> <div class="article-form" style="

我想创建一个编辑链接,在单击文章时更新文章,
在模板中,其结构如下:

  • 后文
    • 文章内容
    • 文章形式
  • 后期菜单
在单击编辑链接之前,我首先将“文章表单”隐藏为

<div class = "col-md-12 post-text" >
    <div class="article-content">
        {{article.content}}
    </div>

    <div class="article-form" style="display: none;">
    <form class="form-horizontal" action="/article/edit/{{ b.id }}" method="POST">
        <div class="form-group">
            <div class="col-sm-12">
                <textarea class="form-control" id="editContent" name="content" rows="10" cols="30"> 
                    {{form.content.value}} 
                </textarea >
            </div>
        </div>
        <div class="form-group" >
            <div class="col-sm-offset-0 col-sm-12">
                <button type = "submit" class = "btn btn-success btn-sm" id = "saveEditBtn"> Save Edits </button>
            </div>
        </div>
    </form> 
    </div><!-- article-form -->
</div>

<div class="post-menu pull-left">
    <a id="editArticleLink" href="{% url 'article:article_edit' article.id  %}">
        <span class="glyphicon glyphicon-edit" aria-hidden="true">edit </span>
    </a>
    <a id="delArticleLink">
        <span class="glyphicon glyphicon-trash" aria-hidden="true">delete</span>
    </a>
</div>

{{article.content}
{{form.content.value}
保存编辑
删去
更新完成并点击提交后,使用Ajax将数据发送到后端,隐藏“文章表单”并显示“文章内容”


$(文件)。准备好了吗(
$(“.post menu a”)。在(“单击”上,功能(e){
e、 预防默认值();
//检索目标所在的最顶层元素
$postText=$(e.target).closest(“.post-text”);
//隐藏文章内容
$postText.find(“.article content”).hide();
//显示文章表单供用户更新
$postText.find(“.article form”).show();
//捕获按钮提交事件
$(“.article form button”)。在(“单击”)上,函数(e){
var content=$postText.find(“textarea”).val();
$.ajax({
类型:“POST”,
网址:,
数据:,
成功:函数(){
//如果保存成功
$postText.find(“.article content”).show();
$postText.find(“.article form”).hide();
},//成功
})//ajax post请求
});//嵌套按钮单击事件
})//单击事件
)//准备好了吗
我的问题是,在ajax的成功中,
$postText.find(“.article content”).show()仍显示未更新的文章


如何在不刷新页面的情况下检索更新的版本?

如果您可以将编辑后的版本发送到服务器。。。你有了新的内容!用它更新
。文章内容
,然后显示

这就是我想的

//capture the button submitting event
$(".article-form button").on("click", function(e){
  var content = $postText.find("textarea").val();
  $.ajax({
    type:"POST",
    url: ,
    data:,  // <-- There is something missing here... I assume it's content.
    success: function(){
      //if saved successfully
      $postText.find(".article-content").html(content).show();  // update before the show!
      $postText.find(".article-form").hide();
    },//success
  })//ajax post request
});
//捕获按钮提交事件
$(“.article form button”)。在(“单击”)上,函数(e){
var content=$postText.find(“textarea”).val();
$.ajax({
类型:“POST”,
网址:,

数据:,//您的意思是在发送ajax请求后,是否要从编辑过的表单更新文章?在发送ajax请求后,
$postText.find(“.article content”).show()是
要简单地显示编辑的数据@seulingFor,这是不可能的。因为django在发送响应时呈现上下文,但在ajax请求后不会加载页面。为此,您可能必须从ajax请求发送其他呈现的响应,并将其直接添加到html中。此外,您还可以选择使用vue或react等前端框架。如果使用它们,您只需在ajax之后为您的文章绑定数据。我不明白…如果您可以将编辑后的版本发送到服务器…您就有了新内容!用它更新
。文章内容
,然后显示。
//capture the button submitting event
$(".article-form button").on("click", function(e){
  var content = $postText.find("textarea").val();
  $.ajax({
    type:"POST",
    url: ,
    data:,  // <-- There is something missing here... I assume it's content.
    success: function(){
      //if saved successfully
      $postText.find(".article-content").html(content).show();  // update before the show!
      $postText.find(".article-form").hide();
    },//success
  })//ajax post request
});