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?

4 Answers 4

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me ... the data parameter fixed it (although I'm not sure how to extract the msg parameter).
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.

9 Comments

this causes another error: {"Message":"Invalid JSON primitive: TestList."
@Joe, also fix the data as I describe in my answer.
Ok, Now I am getting a different error: Message":"Invalid web service call, missing value for parameter: \u0027ListName\u0027."
@Joe, Use firebug or chrome dev tools to look at the exact request being made.
the JSON parameter names must have double quotes. I've updated the answer.
|
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.

2 Comments

I have replaced the code you mentioned above and I am receiving the same error.
@Joe, also change to type: "GET"
0

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

1 Comment

I tried the above solution by adding data: "{'ListName':'TestList'}" and the error remained the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.