Reputation: 37
I'm learning about asp methods via ajax and...
So I tried doing the ff. from this site: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
In JS file:
$(document).ready(function () {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function () {
$.ajax({
type: "POST",
url: "WebForm1.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
In aspx:
<html>
<head>
<title>Calling a page method with jQuery</title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="Scripts/JScript1.js"></script>
</head>
<body>
<div id="Result">Click here for the time.</div>
</body>
</html>
In code-behind:
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string GetDate()
{
return DateTime.Now.ToString();
}
}
}
When I run it on VS studio on my local PC, it works just fine; however, once the files are deployed in IIS server, it gives this error when I click on the container:
POST http:///WebForm1.aspx/GetDate 500 (Internal Server Error)
Is there something to configure in IIS? or is it code-related error?
Upvotes: 0
Views: 5961
Reputation: 4076
Try remove this line [ScriptMethod(UseHttpGet = true)]
Becouse this line is used When you are trying a Get Request, When you see the Example, they are trying to do a POST request and this lines Is not needed.
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
Upvotes: 1
Reputation: 3384
You're doing a POST request to a GET method, try changing your javascript to:
$("#Result").click(function () {
$.get("WebForm1.aspx/GetDate", function (msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
});
});
Upvotes: 1