【问题标题】:calling server side method doesnt work 404 not found调用服务器端方法不起作用 404 未找到
【发布时间】:2015-11-14 00:01:32
【问题描述】:

我的项目(解决方案 -> 项目 -> 模型 -> Server.cs)下名为 Models 的文件夹中有一个名为 server 的类

我有一个简单的按钮,点击后我想调用我的服务器端方法并获取返回的字符串..

我收到错误 404(未找到)。我尝试了几个不同的 url:s 但它似乎不起作用..

index.html:

<body>
<h1>Weeelcome</h1>

<button type="button" id="btnName" style="width: 100px; height: 50px;">Get My Name</button>
<p ID="lblTest"></p>
</body>

我的js:

$(document).ready(function () {
$("#btnName").click(function () {
    $.ajax({
        url: "../../Models/GetMyName",
        type: "GET",
        success: function (result) {
            alert(result);
        },
        error: function (ex) {
            //alert("Error");
        }
    });
});
});

服务器.cs

public class Server
{
    [WebMethod]
    public static string GetMyName()
    {
        return "MyName";
    }
}

错误

GET http://localhost:50603/Models/GetMyName 404(未找到)

【问题讨论】:

  • http://localhost:50603/Models/GetMyName 是您服务器端文件的正确网址吗?尝试在浏览器中打开它
  • @void 好吧,我相信是这样..我不知道如何为文件指出正确的 url..如何获得正确的 url?
  • 如果您感到困惑,请尝试在浏览器中检查不同的组合。
  • 尝试将其设为发布请求
  • 不应该是Models/Server/GetMyName还是Models/Server.cs/GetMyName?我知道当我将 web 方法放在我的 aspx 页面上时,我会使用 NameOfPage.aspx/NameOfMethod

标签: c# jquery ajax


【解决方案1】:

从您的问题中不清楚您正在使用 ASP.NET MVC?因为在这种情况下 index.html 假设为 index.cshtml

其次,如果您使用的是 asp.net mvc,我相信您正在尝试访问模型而不是控制器。您提供了错误的路径(使用文件或位置路径),而您需要提供 服务器路径/您的 Server.cs 路径/您的操作路径

第三个错误是非静态类 server.cs 具有静态功能

第四,如果你在后台使用web service(asmx),并使用html页面与你的web service进行通信,那么下面会提到解决方案,

您需要在Server.cs中声明GetMyName方法,以获取或设置一个指示是否使用HTTP GET调用该方法的值。

   [WebMethod]
   [ScriptMethod(UseHttpGet = true)]

参考:https://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.usehttpget(v=vs.110).aspx

我无法测试这段代码,但我相信这对你有用

index.html

<head>
 <script>
    var __appBasePath = 'http://yourserver.com/';
 <script>
</head>
<body>
<h1>Weeelcome</h1>

 <button type="button" id="btnName" style="width: 100px; height: 50px;">Get My Name</button>
<p ID="lblTest"></p>
</body>

你的 JS

$(document).ready(function () {
$("#btnName").click(function () {
    $.ajax({
        url:  __appBasePath  + "Server/GetMyName",
        type: "GET",
        success: function (result) {
            alert(result);
        },
        error: function (ex) {
            //alert("Error");
        }
    });
   });
});

您的 Server.cs(如果是 Web 服务) 注意:非静态类不能有静态方法:所以去掉静态

public class Server
{
   [WebMethod]
   [ScriptMethod(UseHttpGet = true)]
   public string GetMyName()
   {
       return "MyName";
   }
}

如果您使用的是 ASP.NET MVC 控制器或 Web API:

如果您使用的是 asp.net MVC,那么您做错的事情是您试图调用您在模型中定义的函数。正如我所看到的,您已经提到(解决方案-> 项目-> 模型-> Server.cs)如果您使用的是 asp.net MVC/Web API,那么只需在控制器操作中使用 [HttpGET]。即

Model.cs

public class ServerModel
{
   public string GetMyName()
   {
       return "MyName";
   }
}

如果是 MVC 控制器

public class ServerController : Controller
{
   public JsonResult GetMyName()
   {
       ServerModel model = new ServerModel();
       var name = model.GetMyName;
       return Json(name, JsonRequestBehavior.AllowGet);
   }
}

如果您使用的是 Asp.net mvc web apis,您只需将 ServerController 类中的 Controller 替换为 ApiController。

如果是 WebAPI 替换:

public class ServerController : Controller

public class ServerController : ApiController

【讨论】:

  • 嗨,对不起,如果我不清楚,但我认为我的标签就足够了。我没有使用 MVC。我只是使用一个普通的 html 页面(不是 aspx)。我现在就试试这个。。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多