Datatables Working on Local Host but when published to Web app get error 404

Datatables Working on Local Host but when published to Web app get error 404

davey.cawooddavey.cawood Posts: 11Questions: 2Answers: 0

Hi,
I have an issue when using aspnet.core 2.1, I have datatables controller for my database which connects to the azure hosted sql server database.
This works fine when using IIS Express Localhost however when I publish the project to a web application I get an the following error,
DataTables warning: table id=example - Ajax error. For more information about this error, please see http://datatables.net/tn/7.
The connection string I am trying to use has windows login and I believe this error may be due to the api request (http post and get) as it says the api url cannot be found error 404 or possibly due to the antiforgery token in the ajax request?

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    url cannot be found error 404 or possibly due to the antiforgery token in the ajax request?

    You will need to look at your server logs and configuration to determine the exact cause of the 404. The Cross-Site Request Forgery docs may be helpful.

    Kevin

  • davey.cawooddavey.cawood Posts: 11Questions: 2Answers: 0

    It is still not finding the api Url from the controller?

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    The 404 error is returned from your server. That is the place to look to find out why the server is responding with the 404. The server logs should provide details of why the error is occurring.

    Kevin

  • davey.cawooddavey.cawood Posts: 11Questions: 2Answers: 0

    Yes the server logs show the error is in the ajax request and is now giving a status code 400 Bad Request

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    Do the server logs indicate why its a bad request?

    Maybe this troubleshooting link will help:
    https://airbrake.io/blog/http-errors/400-bad-request

    Kevin

  • davey.cawooddavey.cawood Posts: 11Questions: 2Answers: 0

    No it does not appear to indicate why

  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin

    Could you show me your controller code please? Most likely its something to do with the routing, but I'm not sure what. The controller will be where we can start though.

    Allan

  • davey.cawooddavey.cawood Posts: 11Questions: 2Answers: 0
    edited January 2020

    [Route("/Issues/Data")]
    [HttpGet]
    [HttpPost]

        public ActionResult Issues()
        {
            var dbConnection = Environment.GetEnvironmentVariable("DbConnection");
    
            using (var db = new Database("sqlserver", "dbConnection"))
            {
                var response = new Editor(db, "Issues")
    
                     .Model<IssuesTestModel>()
    
    
                          .Pkey("Issues.Id_Issues")
                     .Field(new Field("Issues.Number").SetFormatter(Format.IfEmpty(null)))
                     .Field(new Field("Issues.Title").SetFormatter(Format.IfEmpty(null)))
                     .Field(new Field("Issues.Description").SetFormatter(Format.IfEmpty(null)))
    
                     .TryCatch(false)
                        .Process(Request)
                        .Data();
                return Json(response);
    
    
    
    
            }
    
        }
    
  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin

    Thank - so if you load:

    {{domain/ip:port}}/Issues/Data

    in your browser, what do you get? Is it still a 404 or 400 error?

    Allan

  • davey.cawooddavey.cawood Posts: 11Questions: 2Answers: 0

    404 error still

  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin

    Okay good - so its a routing issue.

    I guess the key question now is what is the difference between your local ISS Express server and the remote server? What versions of ISS are they running? What versions of .NET Framework, etc?

    Allan

  • paulwingspaulwings Posts: 1Questions: 0Answers: 0

    The 400 Bad Request error is an HTTP status code indicates that the request you sent to the webserver was malformed , in other words, the data stream sent by the client to the server didn't follow the rules. It means that the request itself has somehow incorrect or corrupted and the server couldn't understand it. There are a number of different causes for a 400 Bad Request Error . It might be a malformed request syntax, invalid request message framing, or deceptive request routing . In most cases, the problem is on the website itself, and there's not much you can do about that.

  • anton_miroshkinanton_miroshkin Posts: 1Questions: 0Answers: 0
    edited July 2021

    In my case that was an error linked to the IIS configuration. The app was ASP.NET Core and deployed in virtual directory under the default web app(Instead of localhost:5000 it was deployed in localhost:5000/application). It means that ASP.NET Core links works fine but datatables links were broken(You could try to check this fact using Fiddler). This issue was solved with adding app path to the links in views.

    @model HomeViewModel
    @{string appPath= $"{Context.Request.Scheme}://{Context.Request.Host.Value}{Url.Content("~/")}";}

    and using them in url for datatables

    "ajax": {
    "url": "@(appPath)" + "Uploads/LoadData",
    "type": "POST",
    "datatype": "json"
    }

Sign In or Register to comment.