阿贾克斯返回多个值 [英] Ajax return multiple values

查看:158
本文介绍了阿贾克斯返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,如下图所示,但一个值只在成功时返回返回多个值。

这是我想要做的:

 <脚本类型=文/ JavaScript的>
        $(文件)。就绪(函数(){
                $(#getdetails)。点击(函数(){
                $阿贾克斯({
                    键入:POST,
                    网址:Default.aspx的/ Gettext的
                    数据:JSON.stringify({SampleText:$('#sampletext)VAL(),FontType:$('#fonttype)VAL()})
                    的contentType:应用/ JSON的;字符集= UTF-8,
                    数据类型:JSON
                    成功:函数(MSG){
                        $(#结果)文本(msg.d)。
                    }
                });        $(#FontLists)。改变(函数(){
            $('#fonttype)VAL($('#FontLists选项:选择。)文本());
        });
    });
< / SCRIPT>

HTML

 输入文本:其中,输入ID =sampletext类型=文本/>
<选择ID =FontLists>
    <期权价值=Aharoni> Aharoni< /选项>
    <期权价值=阿尔及利亚>阿尔及利亚< /选项>
    <期权价值=安达卢斯>&安达卢斯LT; /选项>
< /选择>
<输入ID =fonttype类型=隐藏/>

codebehind:

 <的WebMethod()GT; _
< ScriptMethod(ResponseFormat:= ResponseFormat.Json)GT;
公共共享功能的Gettext(BYVAL SampleText作为字符串,BYVAL FontType作为字符串)作为字符串
    返回SampleText
    返回FontType
结束功能


解决方案

您可以设计一类具有两个属性,然后让你的WebMethod返回这个类的一个实例(对不起,如果我做了一些错别字,我的VB.NET技能生锈)。

 公共类为MyModel
    公共属性SampleText为String
    公共属性FontType作为FontType
末级

和再适应你的方法来返回这个模式:

 <的WebMethod()GT; _
< ScriptMethod(ResponseFormat:= ResponseFormat.Json)GT;
公共共享功能的Gettext(BYVAL SampleText作为字符串,BYVAL FontType作为字符串)作为为MyModel
    昏暗的模式=新为MyModel()
    model.SampleText = SampleText
    model.FontType = FontType
    回归模型
结束功能

和客户端,你可以用自己的名字进入2个属性:

 成功:函数(MSG){
    警报(msg.d.SampleText);
    警报(msg.d.FontType);
}

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);
}

这篇关于阿贾克斯返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆