MVC javascript $.post not working on server But works in Localhost

App Dev 86 Reputation points
2022-04-08T20:46:53.847+00:00

Hi All,

I have this line in Index.cshtml

        $.post("../Write/Index", { TypeFilter: TypeID, StatusFilter: StatusID }, function (data) { $("#FilteredQueue").html(data); });

I use this this to help filter and data that is in the index page

This the error that shows that in the F12
jquery-3.3.1.js:9600 POST http://apps/Write/Index 503 (Service Unavailable)

This error only shows up when running on the server
On localhost it work.

In the controller:

    [HttpPost] //See This Error https://stackoverflow.com/questions/33377232/jquery-post-works-locally-but-not-on-server
               //public ActionResult FilteredQueue(int? TypeFilter, int? StatusFilter)
    public ActionResult Index(int? TypeFilter, int? StatusFilter)
    {

        if (((StatusFilter == 2) || (StatusFilter == 6) || (StatusFilter == 5) || (StatusFilter == 8)) && (TypeFilter == null)) //Closed Request Exception  no Status Type Filter Return Everything that was parked Close and Colsed Completed
        {
            var FilteredList = db.WRITE_REQ_REQUEST_TBL.Where(Request => Request.REQ_REQUEST_STATUS_ID == StatusFilter).OrderBy(R => R.REQ_REQUEST_DATE).ToList();
            ViewBag.FilteredResults = FilteredList;

            TypeFilter = null;
            StatusFilter = null;

            return PartialView("~/Views/Write/_FilteredQueue.cshtml", ViewBag.FilteredResults);
        }
        else if (((StatusFilter == 2) || (StatusFilter == 6) || (StatusFilter == 5) || (StatusFilter == 8)) && (TypeFilter != null)) //Closed Request Exception  no Status Type Filter Return Everything that was parked Close and Colsed Completed
        {
            var FilteredList = db.WRITE_REQ_REQUEST_TBL.Where(Request => Request.REQ_REQUEST_STATUS_ID == StatusFilter && Request.REQ_REQUEST_TYPE_ID == TypeFilter).OrderBy(R => R.REQ_REQUEST_DATE).ToList();
            ViewBag.FilteredResults = FilteredList;

            TypeFilter = null;
            StatusFilter = null;

            return PartialView("~/Views/Write/_FilteredQueue.cshtml", ViewBag.FilteredResults);
        }
        else
        {

            if (TypeFilter != null && StatusFilter != null)
            {
                var FilteredList = db.WRITE_REQ_REQUEST_TBL.Where(Request => Request.REQ_REQUEST_STATUS_ID == StatusFilter && Request.REQ_REQUEST_TYPE_ID == TypeFilter && Request.REQ_REQUEST_STATUS != true && Request.REQ_REQUEST_STATUS != false).OrderBy(R => R.REQ_REQUEST_DATE).ToList();

                TypeFilter = null;
                StatusFilter = null;

                ViewBag.FilteredResults = FilteredList;
            }

            else if (TypeFilter != null && StatusFilter == null)
            {
                var FilteredList = db.WRITE_REQ_REQUEST_TBL.Where(Request => Request.REQ_REQUEST_TYPE_ID == TypeFilter && Request.REQ_REQUEST_STATUS != true && Request.REQ_REQUEST_STATUS != false).OrderBy(R => R.REQ_REQUEST_DATE).ToList();

                TypeFilter = null;
                StatusFilter = null;

                ViewBag.FilteredResults = FilteredList;

            }

            else if (TypeFilter == null && StatusFilter != null)
            {
                var FilteredList = db.WRITE_REQ_REQUEST_TBL.Where(Request => Request.REQ_REQUEST_STATUS_ID == StatusFilter && Request.REQ_REQUEST_STATUS != true && Request.REQ_REQUEST_STATUS != false).OrderBy(R => R.REQ_REQUEST_DATE).ToList();

                TypeFilter = null;
                StatusFilter = null;

                ViewBag.FilteredResults = FilteredList;
            }


            //return View(ViewBag.FilteredResults);
            else if (TypeFilter == null && StatusFilter == null)
            {
                var FilteredList = db.WRITE_REQ_REQUEST_TBL.Where(Request => Request.REQ_REQUEST_STATUS_ID != 2 && Request.REQ_REQUEST_STATUS_ID != 5 && Request.REQ_REQUEST_STATUS_ID != 6 && Request.REQ_REQUEST_STATUS_ID != 8 && Request.REQ_REQUEST_STATUS != true && Request.REQ_REQUEST_STATUS != false).OrderBy(R => R.REQ_REQUEST_DATE).ToList();
                /* && (Request.REQ_REQUEST_CLOSE_DATE - DateTime.Today).Value.TotalDays > 10).ToList();*/

                TypeFilter = null;
                StatusFilter = null;

                ViewBag.FilteredResults = FilteredList;

            }
            else
            {
                var FilteredList = db.WRITE_REQ_REQUEST_TBL.Where(Request => Request.REQ_REQUEST_STATUS_ID != 2 || Request.REQ_REQUEST_STATUS_ID != 5 || Request.REQ_REQUEST_STATUS_ID != 6 || Request.REQ_REQUEST_STATUS_ID != 8 && Request.REQ_REQUEST_STATUS != true && Request.REQ_REQUEST_STATUS != false).ToList();

                TypeFilter = null;
                StatusFilter = null;

                ViewBag.FilteredResults = FilteredList;

            }

            return PartialView("~/Views/Write/_FilteredQueue.cshtml", ViewBag.FilteredResults);
        }
    }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,365 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,486 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 59,056 Reputation points
    2022-04-08T20:54:11.683+00:00

    check the url in the network trace. its probably invalid


3 additional answers

Sort by: Most helpful
  1. App Dev 86 Reputation points
    2022-04-08T21:16:01.687+00:00

    Hi Bruce,

    Wow would I do that?

    I am not sure what you mean by network trace sorry.

    Edit: I found it :
    Capturing the network trace file on Google Chrome
    From the Chrome menu bar, select View > Developer > Developer Tools. From the panel that opens, select the Network tab. Look for a round record button in the upper-left corner of the tab, and make sure it is red. If it is grey, click it once to start recording.Apr 21, 2020

    And will it be obvious it is invalid?

    I guess it would show up red. For some reason it's working now... maybe the new published pages had not loaded.
    as all seems well.

    Would be be the method of correcting this?
    What would a valid url look like for this?

    It is just so odd. Thanks

    Update All is working
    THanks for the tip on the netwrok trace thing!

    0 comments No comments

  2. AgaveJoe 26,676 Reputation points
    2022-04-08T21:20:12.773+00:00

    Try a relative reference from the application root.

    $.post("/Write/Index", { TypeFilter: TypeID, StatusFilter: StatusID }, function (data) { $("#FilteredQueue").html(data); });


  3. AgaveJoe 26,676 Reputation points
    2022-09-13T12:43:46.363+00:00

    The slash means "from the application root". Using the slash was just a guess as we cannot see your application's directory structure or MVC routes.

    When I had the 2 dots before the url it works sometimes ... without the 2 dots it does not work at all.

    The .. means up one directory level from the current directory. It makes sense that the .. works on some pages but not others.

    The 503 indicates the URL is pointing to a nonexistent web application. Most likely the IIS application is not configured correctly because both the slash and the .. cause a 503 error. I would expect the slash to either work or cause in a 404 (Not Found). Also, the URL http://apps/Write/Index is rather odd. In a production app I'd expect a domain name, computer name, or IP address not "apps" on the production system.

    0 comments No comments