开发者

Retrieving POST data from jQuery in Django without a form

I am currently able to use the post method of jQuery to a view pointed by myurl

开发者_C百科 jQuery.post("/myurl", {'value1':value1, 'value2':value2,
                        'csrftoken': '{{ csrf_token }}'}, 
             function(data) {
                        alert("Data Loaded: " + data);
                            });

However, I don't know how to retrieve the posted data inside the view without using a form:

 def save_user_graph(request):
      if request.method == 'POST': 
            return HttpResponse(request.POST.get('value1'),status=201)

returns None.


If save_user_graph is returning None, then request.method is not POST. If save_user_graph is returning an HttpResponse with the word 'None' in it, then value1 isn't being sent correctly. Can you clarify which is the case?

If you have chrome, try navigating to the page with the developer toolbar open. You can go to the 'console' tab and view the AJAX request and see what data was sent with it, to make sure that the data was sent correctly.

Similarly, if you have firefox and firebug, you can go to the page and watch the ajax request and check the post parameters.

Assuming that the javascript works, you'll want to log request.POST in save_user_graph. In a pinch, you can just print request.POST and, assuming you're using the django dev server, the output will be printed in the dev server output.

We'll need a bit more debugging info before we can give a better answer. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜