Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
74
rated 0 times [  80] [ 6]  / answers: 1 / hits: 33380  / 9 Years ago, sun, may 3, 2015, 12:00:00

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.

More From » c#

 Answers
2

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

[#66773] Friday, May 1, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
halleyb

Total Points: 604
Total Questions: 96
Total Answers: 115

Location: Tokelau
Member since Wed, Oct 14, 2020
4 Years ago
;