c# - Why is ASP.NET WEB API not returning 200 Ok as my response in ajax -


i have asp.net web api checks if user logged in.when debug 200 ok response api controller when inspect in fiddler 2 200 ok response. 1 empty without object suppose return , other response contain response object.

controller code

 [httppost]     [route("api/bts_admin/login")]     public httpresponsemessage adminlogin(admin admin)     {         admin response = admin.logincheck();         if (response != null)         {             return request.createresponse(httpstatuscode.ok, response);         }         else         {             return request.createerrorresponse(httpstatuscode.notfound, "login failed");         }     } 

also when suppose success response ajax response returned in error section of ajax call

ajax call code

                $('#btnlogin').click(function () {                 $.ajax({                 url: 'http://localhost:61468/api/bts_admin/login',                 method: 'post',                 datatype: json,                 contenttype: 'application/json',                 data: json.stringify({                     email: $('#txtusername').val(),                     password: $('#txtpassword').val(),                 }),                 success: function (response) {                     //localstorage.setitem('accesstoken', response.access_token);                     //localstorage.setitem('username', response.username);                     window.location.href = "data.html";                   },                  error: function (jqxhr) {                     $('#diverrortext').text(jqxhr.responsetext);                     $('#diverror').show('fade');                  }              }); 

note: response

                    error: function (jqxhr) {                     $('#diverrortext').text(jqxhr.responsetext);                     $('#diverror').show('fade'); 

[httppost] [route("api/bts_admin/login")] public httpresponsemessage adminlogin(admin admin) {     admin response = admin.logincheck();     if (response != null)     {         return request.createresponse(httpstatuscode.ok, response);     }     else     {         //ajax error here, because set 404 error (not found.)         return request.createerrorresponse(httpstatuscode.notfound, "login failed");     } } 

try this:

[httppost] [route("api/bts_admin/login")] public ihttpactionresult adminlogin(admin admin) {     admin response = admin.logincheck();     return ok(response); } 

and in ajax request check returned value:

            $('#btnlogin').click(function () {             $.ajax({             url: 'http://localhost:61468/api/bts_admin/login',             method: 'post',             datatype: json,             contenttype: 'application/json',             data: json.stringify({                 email: $('#txtusername').val(),                 password: $('#txtpassword').val(),             }),             success: function (response) {                 if(response){                 //do somethig                   window.location.href = "data.html";                 }else{                     $('#diverrortext').text(jqxhr.responsetext);                     $('#diverror').show('fade');                                }              },              error: function (jqxhr) {                 alert('error');             }         }); 

when in controller set response status code not 200ok, ajax think error, error callback fire up.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -