-1

i have this simple ajax call from client-side : this is in the TestPage.aspx

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Web.Services" %>

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Piece Of Cake </title>
    <link rel="stylesheet" type="text/css" href="../css/navigation.css">
    <link rel="stylesheet" type="text/css" href="../css/form.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    <script type="text/javascript" src="../javascript/validateForm.js"></script>
    <script type="text/javascript" src="../javascript/effects.js"></script>
    <script>  
        $(document).ready(function () {          
            $.ajax({
                type: "POST",
                url: "TestPage/GetName/",
                contentType: "application/json; charset=utf-8;",
                dataType: "json",
                success: function (response) {
                    alert("success")
                },
                failure: function (response) {
                    alert("failure")
                },
                error: ErrorOccur
            });
        });
        function ErrorOccur(data, status, req) {
            alert("error:"+req.responseText + " " + status);
        }
 </script>
</head>

now in the TestPage.aspx.cs file i have :

namespace Foo.html
{
     
    public partial class TestPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var t = 0;
        }

        [WebMethod]
        public static string GetName()
        {
            var chk = new check
            {
                subject = "hello! ",
                description = "13 Years Old"
            };
            return JsonConvert.SerializeObject(chk);
        }
        public class check
        {
            public string subject { get; set; }
            public string description { get; set; }
        }
    }
}

I can debug the cs files like the "RouteConfig.cs" in the app.
but looks like i have a problem debugging the web forms part.
When i try to debug the app after setting breakpoints in the GetName and Page_Load methods the debugger never reaches them. the app running but l keep getting the ajax error that says:

error:undefined parsererror

im using the IIS express ( which comes with VS )
all debug are set in web.config :

<compilation debug="true" targetFramework="4.7.2" />

why i can't debug the app?
why it never reaches GetName method?

UPDATE
I changed the javascript error callback and now im getting the error:

unknown web method GetName
2
  • is this page using Master Pages? i.e. has a MasterPageFile attribute? Commented Mar 25, 2021 at 12:16
  • dont think so all i see in the header is : <%@ Page Language="C#" %> <%@ Import Namespace="System.Web.Services" %> Commented Mar 25, 2021 at 12:33

1 Answer 1

0

A simple example with .NET Framework 4.7.2, the ASPX page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MasterLess.aspx.cs" Inherits="WebForms_DDL_Selection.MasterLess" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <script src='<%= ResolveUrl("~/Scripts/jquery-3.4.1.min.js") %>' type="text/javascript"></script>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
    <script type="text/javascript">
        $(document).ready(function () {
            var name = 'kipi';

            jQuery.ajax({
                url: '<%= ResolveUrl("MasterLess.aspx/GetName") %>',
                type: "POST",
                dataType: "json",
                data: "{'name': '" + name + "'}",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert(data.d);
                }
            });
        });
    </script>
</body>
</html>

And the code behind CS:

protected void Page_Load(object sender, EventArgs e)
{

}
        
[WebMethod]
public static string GetName(string name)
{
    return "Hey there " + name;
}

In case there's a 401 error this needs to be modified in ~/App_Start/RouteConfig.cs (or the line commented out):

settings.AutoRedirectMode = RedirectMode.Off;
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks but still not working i try to add : <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs"%> but i got this error after build: 1>C:\Dev\my\\project\Myproject\html\TestPage.aspx.designer.cs(12,23,12,24): error CS1001: Identifier expected
@user63898 edit the question and add the aspx code
done the edit , you can see the header of the aspx page
is this a WebForms project? With ASPX pages? where are the AutoEventWireup CodeBehind and Inherits attributes on the @Page directive?
is it perhaps a website project? does this project have a global.asax file in the root?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.