Sunday, December 30, 2012

Re: Django jQuery AJAX Self Replacing Script

EDITs made to orginal but same issue.

On Sunday, December 30, 2012 5:16:17 PM UTC-8, That guy wrote:
Hello,


I've been having trouble implementing a self-replacing script. I admit that I am terribly confused.

I am trying to implement two buttons such that if you click on one, a POST request is sent to the server; the server validates that you indeed went to this webpage and sends back a HttpResponseRedirect to a page containing an incomplete web-page/a page that has only the portions I want updated after the button is clicked. Once my orginal jQuery request receives that new page, it will replace the elements on current the page with the new one it just received.

My jQuery looks like:

<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>

Incomplete Page:

<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>

Do I have the right idea? When I watch my requests through the FireBug console, the traffic begins scaling upward everytime the button is clicked.

For instance at the start, it beings with 1 click = 1 POST then 1 click = 2POST, and then 1 click = 3 POST. I'm not too sure why this is happening. Is there something wrong with my jQuery script?

Note: The jQuery script needs to be updated after each POST too.


Thank You for any help you can provide :)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/wVV36UrowMIJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment