現在、ログイン検証システムを実装しようとしています。ユーザーが別のページにリダイレクトされることなく応答を受け取ることができるように、私はajaxを使用しています。私のajax関数は、ユーザーが入力した電子メールとパスワードを送信し、コールバック関数でメッセージを取得します。これは、電子メール、パスワード、または実際のHttpResponseオブジェクトの3つのタイプになります。しかし、ajaxとjqueryを使用して特定のhttp応答オブジェクトをレンダリングする方法がわかりません。location.hrefはオプションですか?以下のコードを貼り付けています。
javascriptの場合:
function loginSubmit(email, password) {
var d= "email=" + email + "&password=" + password;
$.ajax({
url: "/login",
type: "POST",
dataType: "text",
data: d,
success: function(m) {
if (m == "email") {
$("#emailMessage").html("There is no account associated with this email address.");
$("#emailError").show();
$("#emailError").fadeOut(5000, function() {});
} else if (m == "password") {
$("#emailMessage").html("There is no account associated with this email address.");
$("#emailError").show();
$("#emailError").fadeOut(5000, function() {});
} else {
}
}
});
}
ビュー機能で:
def login(request):
json = request.POST
e = json['email']
p = json['password']
u = User.objects.filter(email=e)
if (len(u)):
up = User.objects.filter(email=e, password=p)
if (len(up)):
return render_to_response('profile.html', context_instance=RequestContext(request))
else:
data = "password"
c = RequestContext(request, {'result':data})
t = Template("{{result}}")
datatype=u"application/javascript"
return HttpResponse(t.render(c), datatype)
else:
data = "email"
c = RequestContext(request, {'result':data})
t = Template("{{result}}")
datatype=u"application/javascript"
return HttpResponse(t.render(c), datatype)
ps現在、ダミーテンプレートとHttpResponseを使用して、ajax成功コールバック関数にデータを送信しています。これを達成するためのより効率的な方法はありますか(jsonデータを送り返す)?返信をお待ちしております!