lundi 30 mai 2016

Razor MVC 3 Ajax returns 404

I'm trying to do a basic autocomplete via jquery AJAX, using Razor MVC 3 and Visual Studio 2012 (all of which I had never used before) My code is as follows

JS:

$(document).ready(function () {        
    $("#city").keyup(function () {
        var input = $(this).val();
        getCities(input);
    });
});

function getCities(input) {
    var serviceURL = $("#autocompleteURL").val();    
    var url =
        $.ajax({
            type: "POST",
            url: serviceURL,
            data: {
                'guess': input
            },
            success: function (data) {
                alert('OK!!');    
            },
        });
}

Since i learned that it's not possible to use Razor syntax in a JS file, I'm getting the value from the view via a hidden input, like:

<input type="hidden" id="autocompleteURL" value="@Url.Action("AutoCompleteCity", "Controllers/SearchController")" />

Controller:

 public class SearchController: Controller
   {
       public ActionResult AutoCompleteCity(string guess)
        {
            //database stuff here
        }
   }

I don't know if I'm missing something else. Any help will be greatly appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire