Reputation: 5242
I have a class called server inside a folder called Models right under my project (solution -> project -> Models -> Server.cs)
I have a simple button, and when clicked I wanna call my server side method and get the returned string..
I get error 404 (Not found). I've tried several different url:s but it won't seem to work..
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>
My js:
$(document).ready(function () {
$("#btnName").click(function () {
$.ajax({
url: "../../Models/GetMyName",
type: "GET",
success: function (result) {
alert(result);
},
error: function (ex) {
//alert("Error");
}
});
});
});
Server.cs
public class Server
{
[WebMethod]
public static string GetMyName()
{
return "MyName";
}
}
Error
GET http://localhost:50603/Models/GetMyName 404 (Not Found)
Upvotes: 0
Views: 1173
Reputation: 940
It is not clear from your question that you are using ASP.NET MVC? because in that case index.html suppose to be index.cshtml
Secondly, I believe you are trying to access model not controller if you are using asp.net mvc. You are providing wrong path (using file or location path) whereas, you need to provide Server path/your Server.cs path/Your Action Path.
third mistake is non-static class server.cs is having static function:
Fourth, if you are using web service (asmx) behind the scene and using html page to communicate with your web service, then solution is mentioned below,
You need to declare it GetMyName method in Server.cs to Gets or sets a value that indicates whether to invoke the method by using HTTP GET.
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
I could not test this code but i believe this will work for you
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>
Your JS
$(document).ready(function () {
$("#btnName").click(function () {
$.ajax({
url: __appBasePath + "Server/GetMyName",
type: "GET",
success: function (result) {
alert(result);
},
error: function (ex) {
//alert("Error");
}
});
});
});
Your Server.cs (If It is Web Service) Note: non-static class cannot have static method: so remove static
public class Server
{
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string GetMyName()
{
return "MyName";
}
}
If you are using ASP.NET MVC Controller Or Web API:
If you are using asp.net MVC then what you are doing wrong is you are trying to invoke function which you have defined in model. as i can see that you have mentioned (solution -> project -> Models -> Server.cs) If you are using asp.net MVC/ Web API then simply use [HttpGET] in controller action.i.e.
Model.cs
public class ServerModel
{
public string GetMyName()
{
return "MyName";
}
}
In Case of MVC Controller:
public class ServerController : Controller
{
public JsonResult GetMyName()
{
ServerModel model = new ServerModel();
var name = model.GetMyName;
return Json(name, JsonRequestBehavior.AllowGet);
}
}
You only need to replace Controller to ApiController in ServerController class in case if you are using Asp.net mvc web apis.
In Case Of WebAPI Replace:
public class ServerController : Controller
with
public class ServerController : ApiController
Upvotes: 1