0

这个想法是:我想从 ajax 调用 json 对象并返回它的值。

在我看来,我有这样的事情:

       if user is not None:
            login(request, user)
            link1 = "http://www.google.com"
            link2 = "http://www.yahoo.com"
            json = simplejson.dumps({"link1":link1, "link2":link2})   # I think problem is here
return HttpResponse(json, mimetype="text/json")

和我的jQuery:

function register_user() {
    $("#register-user").click(function() {
        $.ajax({
            url: "/registration/register_user/",
            type: "POST",
            dataType: "text",
            data: {
                username : $("#id_username").val(),
                email : $("#id_email").val(),
                password : $("#id_password").val(),
                password2 : $("#id_password2").val(),
            },
            success: function(data) {
                if (data) {
                    alert(data.link1);  # this returns "undefined"
                }
                else {
                    alert("Error");
                }

            }
        });
    });
4

1 回答 1

1

在你的成功函数中,尝试做parsed_data = $.parseJSON(data),然后你可以使用parsed_data.link1

于 2012-08-17T18:05:41.533 回答