Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I am very new to jQuery and trying to do simple examples using jQuery.ajax
but every time this is showing me the following message

"Microsoft JScript runtime error: 'myMethod' is undefined"

Please guide me where I am lacking.


I have the following HTML code.
<input type="button" onclick="callServer()" value="Test Button" />
    <asp:Label ID="lblTest" runat="server">


the server side code
[WebMethod]
    public void myMethod()
    {
        lblTest.Text = DateTime.Now.ToString();
    }


the jQuery code
function callServer() {
    $.ajax(
    {
    url: "~/jQuery.aspx.cs/"+myMethod,
    contenType:"application/json; charset=utf-8",
    dataType :"json",
    success:onSuccess,
    fail:onFail
    });
}

function onSuccess() {
    alert("Success!!");
}
function onFail() {
    alert("Failed!!");
}
Posted

Have a look at my Tips/Tricks Article on this topic below.(not an advertisement :))
Simplifying Asp.Net Core Ajax

Update - Alternatively, try by changing your url as below.
url: "~/jQuery.aspx/myMethod",
 
Share this answer
 
v2
Comments
Manish Kumar Namdev 27-Oct-11 7:19am    
Thanks for reply.. I used the above url as you mentioned but nothing is happening..
But If I write
type:"GET",
url: "jQuery.aspx/myMethod",

then an alert message appears with "Success!!" message but doesn't show the DateTime.Now.ToString() value on label.
RaisKazi 27-Oct-11 7:51am    
That's because your page does not get re-render in this way. Refer my link. Only change you have to do in my article code is, instead of "alert" use as below.
document.getElementById("<%=lblTest.ClientID%>").innerText = serverDate;
RaisKazi 27-Oct-11 8:26am    
Please refer my link and only change it as below.
function JqueryAjaxExampleResponse(serverDate) {
document.getElementById("<%=lblTest.ClientID%>").innerText = serverDate;
}
Hi,

you have to change some little bit in your code

HTML
function callServer() {
    $.ajax(
    {
    url: "~/jQuery.aspx/"+myMethod,
    contenType:"application/json; charset=utf-8",
    dataType :"json",
    success:onSuccess,
    fail:onFail
    });
}
 
function onSuccess() {
    alert("Success!!");
}
function onFail() {
    alert("Failed!!");
}


All the Best
 
Share this answer
 
Hi,

Check many implementations of page method-

http://delicious.com/anupdg/pagemethod[^]
 
Share this answer
 
Well after searching again on internet I found the answer or my own question.

That we can call only static method through jQuery.ajax() and the method must be enabled with [System.Web.Services.WebMethod]

Now the solution is
Html code
<input type="button" onclick="callServer()" value="Test Button" />
    <asp:label id="lblTest" runat="server" xmlns:asp="#unknown">
</asp:label>


Server side Code
[System.Web.Services.WebMethod]
    public static string myMethod()
    {
        return DateTime.Now.ToString();
    }



And,finally, jQuery
function callServer() {
    $.ajax(
    {
    type:"POST",                      //request type.
    url: "jQuery.aspx/myMethod",     //.aspx page name and method name
    data: "{}",                     //take the parameter if any else blank 
    contenType:"application/json", //content returned from the server side
    dataType :"json",             //data type
    success: function(data){
         $("#lblTest").text(data.d);
    },
    fail:onFail
    });
}

function onFail() {
    alert("Failed!!");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900