错误调用从jQuery的一个JSON WebService时, [英] Error when calling a json webservice from jquery

查看:161
本文介绍了错误调用从jQuery的一个JSON WebService时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经冲刷谷歌寻找这个同样的问题,我似乎无法找到任何帮助。任何援助AP preciated。我已经在C#中创建一个web服务ASMX:

I have scoured Google looking for this same issue and I cannot seem to find any help. Any assistance is appreciated. I have created a webservice asmx in C#:

    [WebMethod]
    [ScriptMethod()]
    public ListObj GetList(string ListName)
    {
        SqlConnection DBConnection = new SqlConnection();
        DBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ToString();
        SqlDataReader DBReader = null;
        SqlCommand query = new SqlCommand("SELECT Lists.Text, Lists.Value FROM Lists WHERE ListName = '" + ListName + "'", DBConnection);
        List<string> text_list = new List<string>();
        List<string> value_list = new List<string>();

        DBConnection.Open();
        DBReader = query.ExecuteReader();
        while (DBReader.Read())
        {
            text_list.Add(DBReader["Text"].ToString());
            value_list.Add(DBReader["Value"].ToString());
        }
        DBConnection.Close();

        return new ListObj(text_list, value_list);
    }

我试图调用使用jquery方式:

i am attempting to call that method using jquery:

      <script type="text/javascript">
        $(document).ready(function () {

            $("#JoeTest").click(function () {
              $.ajax({  
                  type: "POST",
                  url: "/Portals/0/DnnListService.asmx/GetList",
                  data: {ListName: 'TestList'},
                  contentType: "application/json; charset=utf-8",  
                  dataType: "json",  
                  success: function(msg) {  
                      alert("Success: " + msg);  
                  },
                  error: function (msg) {
                      alert("Failed: "+ msg.status + ": " + msg.statusText);
                  }  
              });   
            });

        }); 
    </script>

如果我直接去ASMX我可以输入我的字符串,并取回数据,没有问题: http://screencast.com/t/JQmHYoz5c http://screencast.com/t/xDuMJe7v1A

If I go directly to the asmx I can input my string and get the data back, no problem: http://screencast.com/t/JQmHYoz5c http://screencast.com/t/xDuMJe7v1A

但是,Ajax调用上面返回错误:

However, the ajax call above is returning an error:

{消息:试图调用的方法\ u0027GetList \ u0027使用POST请求,这是不允许的。,堆栈跟踪:在System.Web.Script.Services.RestHandler.GetRawParams (WebServiceMethodData methodData,HttpContext的背景下)\ r \ n将在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的背景下,WebServiceMethodData methodData),ExceptionType:System.InvalidOperationException}

{"Message":"An attempt was made to call the method \u0027GetList\u0027 using a POST request, which is not allowed.","StackTrace":" at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

这是什么问题可能是任何想法?

Any ideas on what the issue may be?

推荐答案

我想它了!

这是我必须做的。在jQuery的,我需要这样的:

Here is what I had to do. In the jQuery I needed this:

            $("#JoeTest").click(function () {
                $.ajax({  
                    type: "POST",
                    url: "/Portals/0/DnnListService.asmx/GetList",
                    data: '{ ListName: "TestList" }',
                    contentType: "application/json; charset=utf-8",  
                    dataType: "json",
                    success: function(msg) {  
                        alert("Success: " + msg);  
                    },
                    error: function (msg) {
                        alert("Failed: "+ msg.status + ": " + msg.statusText);
                    }  
                });   
            });

注意数据和类型参数。 而在C#我需要改变

Notice the data and the type parameters. And in the C# I needed to change

[ScriptMethod()]

[ScriptMethod]

感谢@peanutbutter_lou的解决方案!

Thanks to @peanutbutter_lou for the solution!

这篇关于错误调用从jQuery的一个JSON WebService时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆