0

I am new to MVC, and I am working on a homework project where I need to make ajax call to a controller so I can select data from the database. When I make the ajax call I get 200 HTTP code but the I can't print what's in the success: in the ajax parameters. If anyone knows the solution to this, please help me. Thank you in advance

MVC controller

[HttpGet]
public JsonResult GetPasos(string pasos)
{
    var patnici =  _context.Patnici
        .FirstOrDefaultAsync(m => m.PassporNo == pasos);

    return new JsonResult(patnici);
}

Ajax call:

$(document).ready(function () {
    $("input#PassporNo").on("input", function () {
        var input = document.getElementById("PassporNo").value;
        var regex = new RegExp("^[A-Z0-9]+$");

        if (regex.test(input) && input.length === 8) {
            //$("input#PassporNo").on("input", function () {
            console.log("blabla")
            $.ajax({
                //base address/controller/Action
                url: '/Patnicis/GetPasos/',
                type: 'GET',                 
                data: 
                    //Passing Input parameter
                    {"pasos": $('input#PassporNo').val() }
                ,                   
                success: function (result) {
                    console.log("Rabote ajax");
                },
                error: function (jqXHR, textStatus, errorThrown, data) {
                    alert(jqXHR.status);
                    alert(textStatus);
                    alert(errorThrown);
                    console.log(data)
                }
            });
            return false;
        }
    });
});
David Liang
  • 20,385
  • 6
  • 44
  • 70
  • You are not doing anything with `result`, like console.log(result); – Stefan Nov 03 '20 at 18:07
  • Does this answer your question? [jQuery: Return data after ajax call success](https://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success) – Stefan Nov 03 '20 at 18:08

2 Answers2

0

Change your controller action attribute [GET] to [Route("~/Patnicis/GetPasos/{pasos}")]

And try this:

(document).ready(function () {
        $("input#PassporNo").on("input", function () {
            var input = document.getElementById("PassporNo").value;
            var regex = new RegExp("^[A-Z0-9]+$")
            if (regex.test(input) && input.length === 8) {

                //$("input#PassporNo").on("input", function () {
                alert("blabla")
                $.ajax({
                    //base address/controller/Action
                    url: '/Patnicis/GetPasos/' + input,
                    type: 'POST',
                    
                    data: null,

                    success: function (result) {
                        alert(JSON.Stringify(result));
                    },
                    error: function (jqXHR, textStatus, errorThrown, data) {
                        alert(jqXHR.status);
                        alert(textStatus);
                        alert(errorThrown);
                        alert(data)
                    }
                });
                return false;
            }
        });
    });
Serge
  • 40,935
  • 4
  • 18
  • 45
-1

Solved

The ajax call stays the same, only the action type of the controller changes

[Route("~/Patnicis/GetPasos/{pasos}")]
        public async  Task<Patnici> GetPasos(string pasos)
        {
           

            var patnici =await  _context.Patnici
                .FirstOrDefaultAsync(m => m.PassporNo == pasos);

            return   patnici;
        }