0

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

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>

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

However, the ajax call above is returning an error:

{"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?

Joe
  • 117
  • 1
  • 2
  • 9

4 Answers4

4

I figured it out!

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

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

[ScriptMethod()]

to

[ScriptMethod]

Thanks to @peanutbutter_lou for the solution!

Joe
  • 117
  • 1
  • 2
  • 9
2

Use:

          $.ajax({  
              type: "GET",
              ...
          });   

You also have to pass the data in the correct json format. To verify the value of the data parameter you can use JSON Lint. Change it like this:

data: '{"parameter1" : 1, "parameter2": "string" }'

Note the use of double quotes. Also, by passing the data as a string you bypass the jQuery data serialization.

m0sa
  • 10,712
  • 4
  • 44
  • 91
  • this causes another error: {"Message":"Invalid JSON primitive: TestList." – Joe Apr 05 '11 at 20:59
  • @Joe, also fix the data as I describe in my answer. – tster Apr 05 '11 at 21:04
  • Ok, Now I am getting a different error: **Message":"Invalid web service call, missing value for parameter: \u0027ListName\u0027."** – Joe Apr 05 '11 at 21:09
  • @Joe, Use firebug or chrome dev tools to look at the exact request being made. – tster Apr 05 '11 at 21:12
  • the JSON parameter names must have double quotes. I've updated the answer. – m0sa Apr 05 '11 at 21:15
  • @m0saI have tried changing my data to read **data: {"ListName": "TestList"}** and there was no change. @tster I use firebug all of the time, but I am not sure how to check and see what is being passed. I also have fiddler if that helps. What variable should I add to my watch to see this? – Joe Apr 05 '11 at 21:25
  • have you tried sending the data as a string? data: '{"ListName": "TestList"}', ... ? – m0sa Apr 05 '11 at 21:29
  • @m0sal Yes, I tried that as well and the same error comes up as before: **"Invalid web service call, missing value for parameter: \u0027ListName\u0027."** – Joe Apr 05 '11 at 21:32
  • And what about passing in the parameter directly to the url like: url: "/Portals/0/DnnListService.asmx/GetList?ListName=yadayada" ? – m0sa Apr 05 '11 at 21:35
1

I think you need to do this too:

        $("#JoeTest").click(function () {
          $.ajax({
              ...
              data: "{'ListName': 'TestList'}",
              ...
          });   
        });

I've always had to do that with jquery when calling ASP.NET web services.

tster
  • 17,883
  • 5
  • 53
  • 72
0

Looks a bit like this question, me thinks:
Using JQuery to call a WebMethod

Community
  • 1
  • 1
T4NK3R
  • 4,245
  • 3
  • 23
  • 25
  • I tried the above solution by adding **data: "{'ListName':'TestList'}"** and the error remained the same. – Joe Apr 05 '11 at 20:56