0

我正在尝试返回多个值,如下所示,但一个值仅在成功时返回。

这就是我想要做的:

<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" />

代码隐藏:

<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
4

1 回答 1

1

您可以设计一个具有 2 个属性的类,然后让您的 WebMethod 返回此类的一个实例(对不起,如果我打错了,我的 VB.NET 技能已经生疏了)。

Public Class MyModel
    Public Property SampleText as String
    Public Property FontType as FontType
End Class

然后调整您的方法以返回此模型:

<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

在客户端上,您可以使用它们的名称访问 2 个属性:

success: function (msg) {
    alert(msg.d.SampleText);
    alert(msg.d.FontType);
}
于 2013-02-10T09:20:31.910 回答