4

I have a webmethod like this:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string test(string Name, int? Age)
{
    return "returned value";
}

And the ajax call :

$.ajax({
  type: "GET",
  url: "form.aspx/test",
  data: {'Name': "n1", 'Age': 30},
  contentType: "application/json; charset=utf-8",
  success: function (data) {
    console.log(data);
  }
});


Without parameters/data it works, but when I try to pass some parameters I get this error:

GET http://localhost:55410/test.aspx/test?Name=n1&Age=30
500 (Internal Server Error)


I think this's the detailed exception:

System.ArgumentException: Unknown web method form.
Parameter name: methodName
Den
  • 283
  • 1
  • 9

2 Answers2

1

You need to pass an object instead of a string, and put quotes around n1 to make it a string:

$.ajax({
  type: "GET",
  url: "test.aspx/test",
  data: {'Name': 'n1', 'Age': 30},  // remove quotes & add quotes to n1
  contentType: "application/json; charset=utf-8",
  success: function (data) {
    console.log(data);
  }
});
StaticBeagle
  • 5,070
  • 2
  • 23
  • 34
  • still the same error. `GET http://localhost:55410/test.aspx/test2?Name=n1&Age=30` `500 (Internal Server Error)` – Den May 29 '18 at 23:42
  • @Den whoops sorry since `n1` is a `string` you need to put quotes around it. Fixed my answer – StaticBeagle May 29 '18 at 23:43
  • @Den where is the `test2` in the url coming from? – StaticBeagle May 29 '18 at 23:54
  • sorry, it's a typo. – Den May 29 '18 at 23:57
  • @Den ok gotcha, so `http://localhost:55410/test.aspx/test` works right? – StaticBeagle May 30 '18 at 00:00
  • I don't understand what you mean by "works", but as I mentioned on the question when I use it without the parameters it works. Does it mean that this `http://localhost:55410/test.aspx/test` works? – Den May 30 '18 at 00:08
  • @Den what I meant is that when you type `http://localhost:55410/test.aspx/test` into your browser, do you get back `"returned value"` rendered on your browser. Just sanity check. – StaticBeagle May 30 '18 at 00:11
  • Please check my last update to the question, I added the detailed exception. – Den May 30 '18 at 00:34
  • @Den just something to try, can you remove the line `contentType: "application/json; charset=utf-8",` and try, if that fails, try cleaning the browser cache with ctrl+f5 to see if it's caching the `GET`, clean the project, rebuild it and try again. – StaticBeagle May 30 '18 at 00:59
0

If you want to pass parameters with url you dont need to use data property at all: Just pass them in the url itself like below:

 $.ajax({
  type: "GET",
  url: "form.aspx/test?name=" + yourStringVariabel + "&age=" + yourAgeVariabel,
  contentType: "application/json; charset=utf-8",
  success: function (data) {
    console.log(data);
  }
});

Try with a post and see if it works:

 $.ajax({
  type: "POST",
  url: "form.aspx/test",
  data: JSON.stringify({ name: 'N1', age : 1 }),
  contentType: "application/json; charset=utf-8",
  success: function (data) {
    console.log(data);
  }
});
Rey
  • 3,663
  • 3
  • 32
  • 55
  • same error `GET http://localhost:55410/form.aspx/test?Name=n1&Age=1` `500 (Internal Server Error)` – Den May 30 '18 at 00:00
  • Why is your method static @Den ? – Rey May 30 '18 at 00:02
  • @Den can you please set the type to 'POST' instead of 'GET' and let me know if it works. Now you can use data property like this `data: JSON.stringify({ name: 'N1', age : 1 }),` – Rey May 30 '18 at 00:11
  • @Den make the method static again because in the documentation indicates it needs to be static – Rey May 30 '18 at 00:17
  • @Den refer to this: seems to be a missing feature with the GET for security reasons: https://stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working and this: https://stackoverflow.com/questions/2397466/ajax-get-requests-to-an-asp-net-page-method/2397521#2397521 – Rey May 30 '18 at 00:21
  • But as I mentioned on the question when I use `GET` without the `parameters` it works. Please check my last update to the question, I added the detailed exception. – Den May 30 '18 at 00:34