javascript - ajax post call to MVC action when i use [Authorize] gives status:200 error -
this ajax posts works fine when user authenticated, uanauthenticated user want use [authorize] redirecting login.javascript code below...
$("#favorite").click(function () { var favoritetext = $("#favorite").text(); var status = 1; if (favoritetext == "remove favorite") { status = 0; } else { status = 1; } $.ajax({ url: '@url.action("addfavorite", "movieprofile")', data: { status: status }, datatype: 'json', type: "post", success: function (data) { alert('sucsess'); if (status == 1) { $("#favorite").text("remove favorite"); } else { $("#favorite").text("add favorite"); } }, error: function (xmlhttprequest, textstatus, errorthrown) { alert('readystate:' + xmlhttprequest.readystate + ' status:' + xmlhttprequest.status + ', status text: ' + xmlhttprequest.statustext, +errorthrown); } }); });
and action is..
[httppost] [authorize] public actionresult addfavorite(int status) { //code goes here }
but gives error readystate:4 status:200, status text: ok.
since ajax-call, cannot redirect server. can redirect if it's direct request.
if want redirect on client-side, in error-handler:
error: function (xmlhttprequest, textstatus, errorthrown) { window.location = "/login"; alert('readystate:' + xmlhttprequest.readystate + ' status:' + xmlhttprequest.status + ', status text: ' + xmlhttprequest.statustext, +errorthrown); }
Comments
Post a Comment