开发者

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


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']
    ...


Sure, in your rate_review function you can access request.GET and access those paramters:

/rate/code/?foo=bar

def rate_review(request):
    request.GET['foo']
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜