0

I'm trying to make an AJAX call to a webmethod in my asp.net page and I cannot seem to pass the data over.

This is my AJAX call

$.ajax({
    type: "GET",
    url: "EditView.aspx/GetAllKeywords",
    data: JSON.stringify({
        keywordIds: ['1', '2']
    }),
    contentType: "application/json; charset=utf-8",
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
    },
    complete: function(jqXHR, status) {
        alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
    }
});

and this is my WebMethod

[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string GetAllKeywords(string[] keywordIds)
{
    return "it worked";
}

Everytime I run it, I keep getting this error

"Invalid web service call, missing value for parameter: \u0027keywordIds\u0027."

which indicates to me, it's not able to match my data from my ajax call to the parameter in my webmethod. What am I doing wrong?

Tushar
  • 78,625
  • 15
  • 134
  • 154
Diskdrive
  • 15,875
  • 25
  • 87
  • 148
  • Okay I have a feeling it's got to do with the fact I'm passing in a string array. When I change to GetAllKeywords(string keyword) and pass in data as data: {"keyword" : "3"}, it works okay, but what is the proper way to pass in a string array? – Diskdrive Jul 28 '15 at 05:32
  • Oh, just pass the entire json string and deserialize it in your method? – Raz Jul 28 '15 at 05:35
  • @Raz : I guess I could do that, but surely that's not best practice? There must be a way I can pass a string array as is? – Diskdrive Jul 28 '15 at 05:37
  • 1
    This looks acceptable? http://stackoverflow.com/questions/7971393/passing-array-of-strings-to-webmethod-with-variable-number-of-arguments-using-jq#7972325 – DDan Jul 28 '15 at 05:42

3 Answers3

2

I quickly created a sample application and observed that you should be using POST instead of GET. When I applied the following settings, I was able to hit the GetAllKeywords method and get the response back successfully.

SCRIPT

<script>
        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/GetAllKeywords",
            data: JSON.stringify({
                keywordIds: ['1', '2']
            }),
            contentType: "application/json; charset=utf-8",
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
            },
            complete: function (jqXHR, status) {
                alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
            }
        });
    </script>

C#

Use POST instead of GET

 [WebMethod(EnableSession = true)]
 [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
 public static string GetAllKeywords(string[] keywordIds)
 {
    return "it worked";
 }

DEBUG

Snapshot from debug mode,

enter image description here

Vimalan Jaya Ganesh
  • 4,552
  • 8
  • 39
  • 81
  • Ok I'll try that - I feel a bit uncomfortable using a POST for something that won't actually modify data. Is there any reason why this is? – Diskdrive Jul 28 '15 at 05:48
  • I've verified that it works! Now, I got to ask, why does POST work and GET doesn't???? – Diskdrive Jul 28 '15 at 05:53
  • @Diskdrive, when using GET, the parameters are passed as query string. In that case, I believe you need to remove JSON format from ajax ( 'content type' ) and ScriptMethod (Response format) from C#. Then, you will be able to pass the array values to GetAllKeywords method and get the response back. – Vimalan Jaya Ganesh Jul 28 '15 at 06:06
0

Had you enable get method for webservice on your web.config?

<configuration>
    <system.web>
    <webServices>
        <protocols>
            <add name="HttpGet"/>
        </protocols>
    </webServices>
    </system.web>
</configuration>

https://support.microsoft.com/en-us/kb/819267

Raz
  • 71
  • 4
0

First Create array variable and then stringfy that variable like below:

$(document).ready(function () {
    var keys = new Array();
    keys[0] = "1";
    keys[1] = "2";
    keys[2] = "3";


    $.ajax({
    type: "POST",
    url: "EditView.aspx/GetAllKeywords",
    data: JSON.stringify({keywordIds:keys }),
    contentType: "application/json; charset=utf-8",
    error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " +   textStatus + "\n\nError: " + errorThrown);
    },
    complete: function(jqXHR, status) {
    alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
    }
 });
});
Light
  • 1,084
  • 8
  • 30