I'm trying to return multiple values as shown below but one value is only returning on success.
This is what I'm trying to do :
<script type="text/javascript">
$(document).ready(function () {
$("#getdetails").click(function () {
$.ajax({
type: "POST",
url: "Default.aspx/Gettext",
data: JSON.stringify({ SampleText: $('#sampletext').val(), FontType: $('#fonttype').val()}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#Result").text(msg.d);
}
});
$("#FontLists").change(function () {
$('#fonttype').val($('#FontLists option:selected').text());
});
});
</Script>
HTML:
Enter Text :<input id="sampletext" type="text" />
<select id="FontLists">
<option value="Aharoni">Aharoni</option>
<option value="Algerian">Algerian</option>
<option value="Andalus">Andalus</option>
</select>
<input id="fonttype" type="hidden" />
Codebehind:
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function Gettext(ByVal SampleText As String, ByVal FontType As String) As String
Return SampleText
Return FontType
End Function
You could design a class with 2 properties and then have your WebMethod return an instance of this class (sorry if I made some typos, my VB.NET skills are rusty).
Public Class MyModel
Public Property SampleText as String
Public Property FontType as FontType
End Class
and then adapt your method to return this model:
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function Gettext(ByVal SampleText As String, ByVal FontType As String) As MyModel
Dim model = New MyModel()
model.SampleText = SampleText
model.FontType = FontType
Return model
End Function
and on the client you could access the 2 properties using their names:
success: function (msg) {
alert(msg.d.SampleText);
alert(msg.d.FontType);
}