8

I'm trying to call the webmethod fucntionality using AJAX but unable to get the appropriate results. I have googled my problem and found many solution but those didn't worked for me. Please guide me what I'm doing wrong. Help will be appreciated.

Cheers

Code Snippet

 function checkUserNameExists() {

//initialization
var pagePath = window.location.pathname + "/getUsername";
var value = document.getElementById('control_userName').value;
var dataString = "{ 'value':'" + value + "' }";
$.ajax({
    type: "GET",
    url: pagePath,
    data: dataString,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    error:
            function (XMLHttpRequest, textStatus, errorThrown) {

            },
    success:
            function (result) {
                var flag = true;
                if (result != null) {
                    flag = result.d;
                    if (flag == "True") {
                        alert('okay fine you are good');
                    }
                    else {
                        alert('try again');
                    }
                }
            }
});
 }

Method in Behind Code file

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public string getUsername(string value)
    {
        return "True";
    }

EXCEPTION

 ExceptionType: "System.InvalidOperationException"
  Message: "An attempt was made to call the method 'getUsername' using a        POST request, which is not allowed."
M A.
  • 949
  • 2
  • 12
  • 29

6 Answers6

8

First, it the webmethod is in the page class, and not in a Webservice class, then it should be static.

Second, the data transfered is not really a string, but an object, so change it to:

var dataString = { 'value':  value  };

Third thing, "type" is for older versions of jquery, you should either change your ajax call to:

method: "GET",
url: pagePath,
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",...

Or change the function in the server side to get post calls, by removing the

UseHttpGet = true
ShaharB
  • 186
  • 8
  • yes the method is implemented in the page class. and I have changed it's signature to static but still got the same error. -->GET http://localhost:7492/route/frame_signup/signup.aspx/getUsername?abc 500 (Internal Server Error) – M A. May 03 '15 at 09:53
  • Did you do the other thing? var dataString = { 'value': value }; instead of var dataString = "{ 'value':'" + value + "' }";? Can you post the error here? – ShaharB May 03 '15 at 09:54
  • yes I this the same thing which you have mentioned above but still same results. – M A. May 03 '15 at 10:06
  • okay I have update the Question above and shared the error message too. – M A. May 03 '15 at 10:07
  • What browser are you using? If you are using chrome, then in the developers (F12 to open) you can go to network, and see the full answer. Can you copy the exception written there in response? – ShaharB May 03 '15 at 10:08
  • I already have updated the question and add an expection. please have a look. – M A. May 03 '15 at 10:13
  • OK, so i've editer my answer for the last part. "type" is for older versions, you should use "method" – ShaharB May 03 '15 at 10:19
  • but I was using the GET instead of POST so why I was getting the error message. – M A. May 03 '15 at 10:24
  • 1
    "type" was used in older versions of jquery, so the call ignored it and sent a post instead. You had to change it to "method" – ShaharB May 03 '15 at 10:25
4

Probably you need to add static to your method declaration as below

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string getUsername(string value)
{
   return "True";
}

if this isn't the case, you could F12 the browser->network then click on the error message to see it briefly.

Concerning the reported issue,the problem with get request, try to make it post

Ahmed Alaa El-Din
  • 1,813
  • 1
  • 16
  • 19
  • My console shows the following error : GET http://localhost:7492/route/frame_signup/signup.aspx/getUsername?abc 500 (Internal Server Error) – M A. May 03 '15 at 09:51
  • yes, by clicking on this error during F12 console is opened,it will show the error in details..also you could try to remove the parameters from both client and server side functions to check if the sending parameter isn't the error – Ahmed Alaa El-Din May 03 '15 at 09:57
  • I have edited my question and post the exception. thanks – M A. May 03 '15 at 10:09
  • remove [ScriptMethod(UseHttpGet = true)] as well as make the request a post request and try it – Ahmed Alaa El-Din May 03 '15 at 10:13
  • okay I have removed this and make it POST. But could you please tell me why should we POST the request what if I use GET instead of POST? – M A. May 03 '15 at 10:30
2

The Answer is here :link

the problem is with the annotation I was using the [ScriptMethod(UseHttpGet = true)] which causing the error. just change the value from true to false.

Community
  • 1
  • 1
M A.
  • 949
  • 2
  • 12
  • 29
0

This is not specific to this question but you can check a few things to get better understanding of what is causing the issue.

  • Check the status code and type of request in the General section of Request Headers.
  • Check the Response Headers if there is any json error.
  • Check the Response to get the message, stacktrace and exception type.
  • Check the Request Payload section in Headers for parameters passed.

Check this post for more detail.

CodeZila
  • 919
  • 2
  • 14
  • 31
0

In my case the problem was the "data" field (both cases, GET and POST). As a test, remove the "data" from the AJAX call and also remove the web method parameter, if it works, then the problem is the format of the "data" field:

$.ajax({
   type: "GET",
   url: pagePath,
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   ...

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string getUsername()
{
    return "True";
}

Some examples:

data: JSON.stringify({ "parameter": variable })     WORKS
data: JSON.stringify({ parameter: variable })       WORKS
data: '{"parameter": "' + variable + '"}'           WORKS
data: '{parameter: ' + variable + '}'               don't works 
data: JSON.stringify({ 'parameter': 'value' })      WORKS
data: '{"parameter":"value"}'                       WORKS
data: "{'parameter':'value'}"                       WORKS
data: '{parameter:value}'                           don't works
data: {parameter:value}                             don't works
data: {"parameter":"value"}                         don't works
data: {'parameter':'value'}                         don't works
Milko
  • 71
  • 5
0

in my case the error was due to incorrect parameter names was defined in jquery ajax call.

Do check the parameter name, also make sure the webmethod function is defined as static in .cs page.

Vikrant Satpute
  • 123
  • 1
  • 5