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
9
  • 1
    the error message listed that the url of the method is 'test2' not 'test' is this a typo or something else? Commented May 29, 2018 at 23:51
  • @AmrElgarhy sorry, it's a typo. Commented May 29, 2018 at 23:57
  • @AmrElgarhy Please check my last update to the question, I added the detailed exception. Commented May 30, 2018 at 0:35
  • Possible duplicate of ASP.NET jQuery error: Unknown Web Method Commented May 30, 2018 at 0:36
  • @AmrElgarhy My web method is public and static Commented May 30, 2018 at 0:39

2 Answers 2

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);
  }
});
Sign up to request clarification or add additional context in comments.

9 Comments

still the same error. GET http://localhost:55410/test.aspx/test2?Name=n1&Age=30 500 (Internal Server Error)
@Den whoops sorry since n1 is a string you need to put quotes around it. Fixed my answer
@Den where is the test2 in the url coming from?
sorry, it's a typo.
@Den ok gotcha, so http://localhost:55410/test.aspx/test works right?
|
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);
  }
});

6 Comments

same error GET http://localhost:55410/form.aspx/test?Name=n1&Age=1 500 (Internal Server Error)
Why is your method static @Den ?
@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 }),
@Den make the method static again because in the documentation indicates it needs to be static
@Den refer to this: seems to be a missing feature with the GET for security reasons: stackoverflow.com/questions/2651091/… and this: stackoverflow.com/questions/2397466/…
|

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.