calling webmethod from jquery C# returning 404 error in .net aspx page

爱⌒轻易说出口 提交于 2019-12-01 10:55:44

问题


must be missing something basic here. I am getting a 404 error calling a webmethod from jquery in an aspx page I'm running locally.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="empJquery.aspx.cs" Inherits="webApiOracle.empJquery" %>

<!DOCTYPE html>

<html lang="en">
<head runat="server">
    <title>jquery emps</title>
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <link href="../Content/bootstrap.min.css" rel="stylesheet" />
    <script type="text/javascript">


        $(document).ready(function () {
            getEmployees();

        });

        function getEmployees() {
            jQuery.ajax({
                url: 'empJquery.aspx/Test',
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function () {
                    alert("Start!!! ");
                },
                success: function (data) {
                    alert("a");
                },
                error: function (msg) { alert("Sorry!!! "); }

            });
        }

    </script>

then in the code behind page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webApiOracle.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace webApiOracle
{
    public partial class empJquery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Services.WebMethod]
        public static string Test()
        {

            return "Success";
        }

    }

}

I'm getting a 404 error whenever I run I attempted changing url to /empJquery.aspx/Test I also tried localhost/empJQuery.aspx/Test and localhost:48784/empJquery.aspx/Test but I can't seem to get it. Do I need to add something to my web config? I'm running .net 4.5 thanks


回答1:


Ok, I just found it my app is a hybrid MVC, Web API, ASPX solution and my route configs were messing me up so I had to add:

 routes.IgnoreRoute("{page}.aspx/{*webmethod}");


来源:https://stackoverflow.com/questions/29860304/calling-webmethod-from-jquery-c-sharp-returning-404-error-in-net-aspx-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!