Django和AJAX:完全迷失了

时间:2014-10-13 20:42:24

标签: jquery python ajax django

我希望以更合适的方式提出问题:

essai.html:

<html>
<head>
  <title> AJAX </title>,
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script type="text/javascript" src="/static/js/ajax.js"></script>
</head>
<body>
  <div class="wrapper">
    <h3>Ajax Callback Introduction</h3>
    <div class='entry_wrapper'>
      <h4>Data to send back to server:</h4>
      <input type="text" size="50" id="input_text"/>
      <input type="button" value="Ajax Call Back" id='call_back_btn'/>
    </div>

    <div class='response_wrapper'>
      <textarea id='responseText'></textarea>
    </div>

ajax.js:

$(document).ready(
  function()
  {

    $('#call_back_btn').click(function()
    {

      $.post("{% url 'friend' %}",
      {

        person_name: $("#input_text").val()
      },
        function(data)
        {
          $('#responseText').val(data)
        }
      );

    });

  }
);

views.py:

from django.http import HttpResponse
def friend(request):
  if request.method == 'POST':
    nom=request.POST.get("person_name")
    return HttpResponse (nom)

urls.py:

from django.conf.urls import patterns, url
from planner import views
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^essai/', views.essai, name='essai'),
    url(r'^friend/', views.friend, name='friend'),
)

问题: 当我点击Ajax Call Back按钮时:如果我没有弄错,发送文本是因为我在firebug中得到以下结果:

http://127.0.0.1:8000/planner/essai/%7B%%20url%20%27friend%27%20%%7D 200 OK 13ms

并在邮政区域:

person_name尝试

但是,文本不会返回到文本区域(id =&#39; responseText&#39;)。相反,我得到了essai.html该字段的全部内容:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<!--CSS-->
<link href="/static/css/bootstrap.css" rel="stylesheet" type="text/css" />

       ...

0 个答案:

没有答案