【问题标题】:how can i use django-tokenapi through jquery ajax?我如何通过 jquery ajax 使用 django-tokenapi?
【发布时间】:2015-01-05 19:25:48
【问题描述】:

我正在尝试使用 curl 命令访问链接 curl -d "user=1&token=3wk-e7621e890e5b8db28948" http://task.woo.gy/api4/index.json 它工作正常 输出是

{"test1": 49, "test2": "awesome", "success": true}

但我不知道如何在 $.ajax 中使用这个 api

当我直接访问此链接时 http://task.woo.gy/api4/index.json

它给了我消息“必须在请求中包含 'user' 和 'token' 参数。”

创建索引的代码

from tokenapi.decorators import token_required
from tokenapi.http import JsonResponse, JsonError

@token_required
def index(request):
    if request.method == 'POST':
        data = {
                'test1': 49,
                'test2': 'awesome',
        }
        return JsonResponse(data)
    else:
        return JsonError("Only POST is allowed")

【问题讨论】:

    标签: jquery python ajax django django-views


    【解决方案1】:

    假设您没有被同源策略阻止,请尝试

    $.ajax({
        url: "http://task.woo.gy/api4/index.json",
        type: "POST",
        data: { 
           user: "1",
           token: "3wk-e7621e890e5b8db28948", 
        }
    });
    

    【讨论】:

    • 感谢您的帮助。
    猜你喜欢
    • 2015-01-05
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2014-05-01
    • 2020-10-18
    • 2010-12-04
    相关资源
    最近更新 更多