当 ajax 在 ASP.net 中发布时,消息显示未定义

发布于 2025-01-20 21:36:59 字数 683 浏览 2 评论 0原文

您好,我在编码和 ASP.NET 方面相当陌生,我遇到了一个错误,如果可以的话请帮助我。这是我的脚本,当我使用 onclick 函数运行下面给出的代码时,成功消息显示为未定义,并且我放置在断点中的 C# 函数 SaveData 没有命中。显然该帖子没有到达服务器功能。

text1和text2是

$.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);
    },
});

服务器端的两个变量

[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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2025-01-27 21:36:59

我已经创建了这个示例并且工作正常。请检查下面的答案。

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

后端

[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 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.

唯憾梦倾城 2025-01-27 21:36:59

好的,我找到了我问题的答案。问题在于在 RouteConfig.cs 文件中评论 app_start 文件夹中的一个代码。

 //settings.AutoRedirectMode = RedirectMode.Permanent;

不知道为什么,但是此后效果很好。请让我知道这是如何工作的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文