I want to use jQuery to make a call to a c# web service called c.ashx which checks whether that username is valid and returns an error message as a string.
What should I put for data: and content type: if the return value of the c# webservice is a string value?
jQuery.ajax({
type: "GET",
url: "/services/CheckUserName.ashx",
data: "",
contenttype: "",
success: function (msg) {
alert("success");
},
error: function (msg, text) {
alert(text);
}
});
I have created a .asmx file instead, but it doesn’t get called by the jQuery. Is the following correct?
jQuery.validator.addMethod("UsernameCheck", function (value, element) {
jQuery.ajax({
type: "POST",
url: "/services/CheckUsername.asmx?CheckUsername",
data: '{ "context": "' + jQuery("#username").value + '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert("success");
},
error: function (msg, text) {
alert(text);
}
});
});
Data should contain the parameters of the method you are calling in a web service. However, the ashx extension is for an HTTP Handler, which is not a good choice for this scenario. A web service should by used instead.
So if you were calling
/services/LoginServices.asmx?CheckUserName, and CheckUserName.asmx had a webmethodValidateUsersuch asthen the
dataattribute for jQuery would beyour
contentTypeshould beapplication/json; charset=utf-8, anddataTypeshould be"json".Note that you’re not going to call
/services/CheckUserName.asmx, the name of the method in the web service has to be appended to the webservice url,/services/LoginServices.asmx?CheckUserName.Also, you’ll need to change your
typeto"POST".Here’s a complete example:
Hope this helps