如何使用 jquery/ajax 将参数传递给 ASP.NET 页面方法?

发布于 2024-07-13 07:56:42 字数 349 浏览 4 评论 0原文

我正在尝试将参数传递给我的静态 Web 方法(位于 asp.net 页面中)。 我试图传递值为“myvalue”的“test1”参数。 关于我做错了什么有什么想法吗?

$.ajax({
    type: "POST",
    url: "WebForm1.aspx/WebMethod1",
    data: {"test1": "myvalue"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg.d);
    }
});

I'm trying to pass parameters to my static web method (which is in an asp.net page). I'm trying to pass the "test1" param with a value of "myvalue". Any ideas on what I am doing wrong?

$.ajax({
    type: "POST",
    url: "WebForm1.aspx/WebMethod1",
    data: {"test1": "myvalue"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg.d);
    }
});

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

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

发布评论

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

评论(3

丘比特射中我 2024-07-20 07:56:42

我的“数据”部分是错误的。 它需要是“{'test1':'myvalue'}”

my "data" section was wrong. it needs to be "{'test1':'myvalue'}"

要走就滚别墨迹 2024-07-20 07:56:42

你遇到了什么错误?

我以前使用过原型(类似于ajax 的jQuery),并且您不引用参数名称。 所以你的数据参数可能应该是:

data: {test1: "myvalue"}

尝试一下。

您还可以尝试设置 Fiddler 并查看发出的实际请求。

What error are you getting?

I've used prototype before (similar to jQuery for ajax) and with that you don't quote the parameter names. So your data parameter should probably be:

data: {test1: "myvalue"}

Give that a shot.

You could also try setting up Fiddler and see the actual request being made.

没有心的人 2024-07-20 07:56:42

按照您的设置方式,ajax 调用中可能发生的任何错误都会被默默地吞噬。 我建议添加如下错误回调:

$.ajax({
    type: "POST",
    url: "WebForm1.aspx/WebMethod1",
    data: {"test1": "myvalue"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg.d);
    },
    error: function(response) {
        $('body',document).html(response.responseText);
    }
});

另外,如果您使用的是 Visual Studio,则可以在调试模式下运行 ASP.NET 应用程序以捕获任何服务器端错误。 您还可以在服务器端代码中的某个位置放置一个断点,以确保它被命中,并检查 Request.Form 集合。

希望这可以帮助。

The way you have it set up, any error that may occur in the ajax call will get silently swallowed. I'd suggest adding an error callback like this:

$.ajax({
    type: "POST",
    url: "WebForm1.aspx/WebMethod1",
    data: {"test1": "myvalue"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg.d);
    },
    error: function(response) {
        $('body',document).html(response.responseText);
    }
});

Also, if you are using Visual Studio, you could run your ASP.NET app in debug mode to catch any server-side error. You can also put a breakpoint somewhere in the server-side code to make sure it's getting hit at all, and to inspect the Request.Form collection.

Hope this helps.

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