简体   繁体   English

Ajax调用webmethod在asp.net中未成功

[英]ajax call to webmethod doesn't hit success in asp.net

i have an ajax request in a page say Test.aspx 我在页面上说一个Test.aspx的ajax请求

  $.ajax({
                type: "POST",
                url: "Test.aspx/TestMethod",
                data: "{ 'Parameter':'Parameter'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (res) {
                    alert('success');
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('error getting Regions');
                }
            });

and in the code behind 并在后面的代码中

   [WebMethod()]
    public static string TestMethod(string Parameter)
    {
      return "teststring";
    }

Issue is control is not going in to success inside ajax, any idea?'' 问题是控制权不会在ajax内取得成功,知道吗?''

Testtry this 测试这个

$.ajax({
                type: "POST",
                url: "Test.aspx/TestMethod",
                data: "{ 'Parameter':'Parameter'}",
                contentType: "application/json; charset=utf-8",
                success: function (res) {

                    var s = JSON.stringify(res);
                    alert(s);
                },
                error: function () {
                    alert('error getting Regions');
                }
            });

There are few things you need to make sure you are doing. 您只需要做几件事就可以确保自己在做。

First thing you need to add script manager with enable page method property true like below. 首先,您需要添加具有启用页面方法属性true的脚本管理器,如下所示。

<asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

See my blog post about How to call ASP.Net page method with jQuery 请参阅我的博客文章有关如何使用jQuery调用ASP.Net页面方法

Check the Page URL and everything. 检查页面URL和所有内容。 It should be correct and should be exactly same as page name and method name as it is case sensitive. 它应该正确,并且应该与页面名称和方法名称完全相同,因为它区分大小写。

Check parameter also. 还要检查参数。 There is another good post with parameter is also below for your reference. 下面还有一篇带参数的好帖子供您参考。

http://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx http://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM