'message shows undefined when ajax post in ASP.net

Hi I'm Fairly new at coding and in asp.net and had an error occurred to me please help me if you can. This is my script when I run the code given below using an onclick function the success message shows as undefined and c# function SaveData which I placed in break point doesn't hit. clearly the post didnt reach the server function.

text1 and text2 are two variables

$.ajax({
    type: "post",
    contentType: "application/json; charset=utf-8",
    url: "Default.aspx/SaveData",
    data: "{dataval: '" + text1 + "', dataval1: '" + text2 + "'}",
    dataType: "json",
    success: function (data) {
        alert(data.d);
    },
});

server side

[WebMethod]
public static string SaveData(string dataval, string dataval1)
{
    string data = String.Format("Your Name is {0} and address is {1}", dataval, dataval1);
    return data;
}


Solution 1:[1]

Ok I have found the answer to my problem. The problem was with commenting a piece of code in RouteConfig.cs file in App_Start folder.

 //settings.AutoRedirectMode = RedirectMode.Permanent;

Don't know why but it worked fine after this. Please let me know how this works.

Solution 2:[2]

I have created this sample and working fine. Please check below answer.

$.ajax({
        type: "GET",
        url: "/Home/SaveData",
        data: { dataval: 'asd', dataval1: 'asd' },
        dataType: "json",
        success: function (data) {
            debugger
            alert(data);
        },
        });
    });

Backend

[HttpGet]
    public ActionResult SaveData(string dataval, string dataval1)
    {
        string data = String.Format("Your Name is {0} and address is {1}", dataval, dataval1);
        return Json(data, JsonRequestBehavior.AllowGet);
    }

I hope this helped you out.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sreeraj j
Solution 2