I am trying to use the simple ajax script to webmethod as follows:
<script type="text/javascript">
$(document).ready(function () {
$("#btnretreive").click(function () {
$.ajax({
type: "POST",
url: "Default.aspx/Gettext",
data: {inputtext: $('#sometext').val()},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#Result").text(msg.d);
}
});
});
});
</script>
And this is my webmethod:
<WebMethod()> _
Public Shared Function Gettext(ByVal inputtext As String) As String
Return inputtext
End Function
Here is my HTML part:
<input id="sometext" type="text" />
<input id="btnretreive" type="button" value="button" />
<div id="Result"></div>
Now my problem is I’m unable to send the input text and receive it back.Can anyone point out the mistake I’m doing here.
You need to use the ScriptMethod attribute in your Web Method in order to return Json.
More information: http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx
Note: you must keep the WebMethod attribute too as shown in MSDN.
Also, you need to convert the Json object you’re passing to the WebMethod to string, for example:
Hope it helps.