c# - 从jQuery C#调用webmethod在.net aspx页面中返回404错误

标签 c# jquery asp.net webmethod

在这里一定缺少一些基本的东西。我在本地运行的aspx页面中从jQuery调用webmethod时遇到404错误。

<%@ 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>


然后在页面后面的代码中

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";
        }

    }

}


每当我运行时,我都遇到404错误,尝试将URL更改为/empJquery.aspx/Test,我也尝试了localhost / empJQuery.aspx / Test和localhost:48784 / empJquery.aspx / Test,但我似乎无法获取它。我是否需要在Web配置中添加一些内容?我正在运行.net 4.5
谢谢

最佳答案

好的,我发现它的应用程序是混合的MVCWeb APIASPX解决方案,并且我的路由配置让我很困惑,所以我必须添加:

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

关于c# - 从jQuery C#调用webmethod在.net aspx页面中返回404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29860304/

相关文章:

c# - 如何从mysql表中删除单个值

jquery - 如果 jQuery 函数在其完成回调中调用自身,这会对堆栈造成递归危险吗?

c# - 如何使用包含资源值的IController Execute()返回View?

c# - Selenium WebDriver - 如何使用 C# 设置页面加载超时

c# - SemaphoreSlim.WaitAsync 在 try block 之前/之后

c# - 如何在以抽象类为根的树层次结构中更正实现 ICloneable?

javascript - 无法让第二个 javascript 函数工作?

javascript - 将 HTML 输入更改为 textarea 并复制所有事件

asp.net - 使用 ASP.NET 动态数据/LINQ to SQL,如何让两个表字段与同一个外键有关系?

asp.net - 在 ASP.NET 中,如何处理 session 和多个选项卡?