Consume API Rest in C# by Ajax in jquery

Asked

Viewed 510 times

0

Hello I have a C# API that Returns a JSON object Follows the code

[HttpGet]
        public string Login(string Pass, string User)
        {
            Return ret = new Return();
            try
            {
                UsuarioDAL DAL = new UsuarioDAL();
                var Usuario = DAL.ValidarLogin(Pass, User);

                if (Usuario.CPF != null)
                {
                    ret.OK = true;
                    ret.obj = Usuario;
                }
                return JsonConvert.SerializeObject(ret);

            }
            catch (Exception Ex)
            {
                return JsonConvert.SerializeObject(ret);
            }
        }
    }

I made a JQUERY call using AJAX for this method

    var Login = function (Fn) {
  switch(Fn){
      case 'logar':
          var USER = $('#Name').val();
          var PASS = $('#Pass').val();
          ValidLogin(USER,PASS);
  }
};


function ValidLogin(Us,Ps){
    $.ajax({
        url: 'http://localhost:49982/api/values/Login?Pass=' + Ps + '&User=' + Us + '',
        type: 'GET',
        dataType: 'json'
    }).done(function (data) {
        console.log(data);
    }).fail(function (data) {
        console.log(data);
    });
}
$(document).ready(function () {
    $('#Acesso').click(function () {
        Login('logar');
    });
});

I did the debug and the url actually calls the API returns the Data in JSON only that jquery returns the error to me Referenceerror of the date object I placed to display on the console

  • 1

    dataType: 'xml' and the server returns in json, the correct would be dataType: 'json'

  • there is yes. is forgot to change this part, was testing in xml, but the error persists, disregard xml.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.