简体   繁体   中英

Razor pages text autocomplete error 404 not found

I have looked at numerous examples of implementing the jQuery autocomplete feature. Most of them cause:

jquery-1.10.2.js:8706 GET /AddApplication/GetURL?term=s 404 (Not Found)

I'm not super experienced with jQuery, I assume this error means the path is wrong?

I'm using c# and razor pages.

AddApplication.cshtml.cs class:

    [System.Web.Mvc.HttpGet]
    public System.Web.Mvc.JsonResult GetURL(string term)
    {

        System.Web.Mvc.JsonResult result = new 
        System.Web.Mvc.JsonResult();

        var urls = (from c in db.Url
                    where c.UrlName.StartsWith(term)
                    select new {c.UrlName});

        result.Data = urls;
        result.JsonRequestBehavior =  JsonRequestBehavior.AllowGet;
        Console.WriteLine("---------"+result);
        return result;
    }

AddApplication.cshtml razor front-end:

<div>
     <input type="text" name="url" id="url" placeholder="Search for a URL" 
      autocomplete="on">
</div>

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/start/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<script type="text/javascript">
    $(function () {
        $("#url").autocomplete({
            source: "@Url.Action("GetURL","AddApplication")",
            minLength: 1,
            select: function (event, ui) {
                if (ui.item) {
                    $("#url").val(ui.item.value);
                    $("form").submit();
                }
            }
        });
    });
</script>

This is as far as I have gotten, I've tried with the ajax function in my script and still didn't work.

Thanks to Rory for providing me with a fix.

Changing source from source:"/AddApplication/GetURL"

to

source: "@Url.Action("GetURL","AddApplication")"

this resolves the 404 not found error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM