Bonky Zhu
If someone is able to show me that what I think or do is not right, I will happily change, for I seek the truth, by which no one was ever truly harmed. It is the person who continues in his self-deception and ignorance who is harmed.

jQuery+AJAX+Django 的使用

一个小小的例子,目的是不刷新页面,将输入框里面的字符串用 base64 编码输出出来

jQuery+ajax 写法


view 函数的写法

@csrf_exempt    # 如果ajax使用POST方法需要使用这个(免除csrf的限制),GET 则不需要
def get_base(request):
    string = request.POST.get('string') # 如果是 GET 方法直接改成GET、就好了
    decode_string = base64.b64encode(string)
    return HttpResponse(json.dumps({"result":decode_string}), content_type='application/json')  #   返回类型一定要和 ajax 里面的 dataType 相对应,content_type 要符合
Share

You may also like...

发表评论