Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django passing parameter with AJAX

I am trying to pass parameters to Django view but I couldn't decide what is the best way to do it.

I am making an AJAX call and my javascript code is

url = "/review/flag/code/"+ code +"/type/" + type + "/content/" + content + "/";
$.getJSON(url, somefunction);

The corresponding url for this call

(r'^rate/code/(?P<code>[-\w]+)/type/(?P<type>[-\w]+)/content/(?P<content>[-\w]+)/$', 'project.views.flag_review'),

And I can get the parameters in my view such that

def rate_review(request,code,type,content):
    ....

My question is because content comes from a textarea it may involve many escape chars and passing it like above causes problems due to regular expression.

Is there any way if I pass the parameters like www.xx.com/review/rate?code=1&type=2&content=sdfafdkaposdfkapskdf... ?

Thanks

like image 806
brsbilgic Avatar asked Feb 24 '23 18:02

brsbilgic


1 Answers

If the content variable is input through a textarea input field in a form, you should probably be using the HTTP POST method for submitting the data. You would get something like:

 url = "/review/flag/code/"+ code +"/type/" + type;
 $.post(url, {'content': content}, somefunction, 'html');

 (r'^rate/code/(?P<code>[-\w]+)/type/(?P<type>[-\w]+)/$', 'project.views.flag_review'),

def rate_review(request,code,type):
    content = request.POST['content']
    ...
like image 104
André Caron Avatar answered Feb 26 '23 22:02

André Caron