0

国に基づいた言語を取得するためにサービスを呼び出しています。複数の言語を返す場合、次のコードは正常に機能しています。ただし、1つのレコードのみを返す場合、出力は正しく行われません(「未定義」として表示されます。アドバイスをお願いします。

これが私のコードです:

$.ajax({
            type: 'POST',
            url: "http://mymethod",
            complete: function () { $.mobile.hidePageLoadingMsg(); },
            dataType: "json",
            contentType: 'application/json',
            data: JSON.stringify({
                "country": countryCode
            }),
            success: function (output, textStatus, jqXHR) {
                $.each(output.geographyInfo, function (i, theItem) {
                    try {
                        languages.push("<option value='" + theItem.languageCode + "'>" + theItem.languageName + "</option>");
                    }
                    catch (error) {
                        alert(error);
                    }
                });
                $(languages.join('')).appendTo("#dpdLanguages").trigger("create");
            }
        });

これがjsonの出力です。

これは、1つの言語のみの場合の出力です。

{"geographyInfo":{"active": "true"、 "countryCode": "US"、 "countryName": "UNITED STATES"、 "instanceID": "1"、 "languageCode": "EN"、 "languageName" :"English"、 "regionName": "North America"}、 "reasonDetails":null、 "size": "1"、 "status": "true"}

そして複数の場合

{"geographyInfo":[{"active": "true"、 "countryCode": "CA"、 "countryName": "CANADA"、 "instanceID": "1"、 "languageCode": "EN"、 "languageName" :"English"、 "regionName": "North America"}、{"active": "true"、 "countryCode": "CA"、 "countryName": "CANADA"、 "instanceID": "1"、 "languageCode ":" FR "、" languageName ":" French "、" regionName ":" North America "}、{" active ":" true "、" countryCode ":" CA "、" countryName ":" CANADA "、" instanceID ":" 2 "、" languageCode ":" EN "、" languageName ":"英語 "、"regionName ":" Americas "}、{" active ":" true "、" countryCode ":" CA "、" countryName ":" CANADA "、" instanceID ":" 2 "、" languageCode ":" FR "、" languageName ":" French "、" regionName ":" Americas "}]、" reasonDetails ":null、" size ":" 4 "、" status ":" true "}

はいudidu、私はそれが事実だと思います..しかし、これを解決する方法は?

4

1 に答える 1

1

ここでの最も簡単な解決策は、次のように、[]でラップするgeographyInfo要素が1つしかない場合です。

{"geographyInfo":[{"active":"true","countryCode":"US","countryName":"UNITED STATES","instanceID":"1","languageCode":"EN","languageName":"English","regionName":"North America"}],"reasonDetails":null,"size":"1","status":"true"}

このため、1つの要素の配列として機能し、コードを変更する必要はありません。現在、geographyInfo要素が1つしかない場合、各ループは内部のgeographyInfoアイテムを個別の配列要素として表示し、7回ループします。

実例は次のとおりです。

index.html:

<!DOCTYPE html>
<html>
<head>
    <title>jQM Complex Demo</title>
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0"/>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>    
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
    <script>
        $(document).on('pagebeforeshow', '#index', function(){     
            $('#populate-button').on('click',function(e,data){     
                $('#dpdLanguages').empty()
                $.ajax({
                    type: 'POST',
                    url: "json.php",
                    complete: function () { $.mobile.hidePageLoadingMsg(); },
                    dataType: "json",
                    contentType: 'application/json',
                    data: "",
                    success: function (output, textStatus, jqXHR) {
                        $.each(output.geographyInfo, function (i, theItem) {
                            try {
                                $('<option>').attr('value',theItem.languageCode).html(theItem.languageName).appendTo("#dpdLanguages");
                            }
                            catch (error) {
                                alert(error);
                            }
                        });
                        $('#dpdLanguages').selectmenu();  
                    }
                });
            });        
        });
    </script>
</head>
<body>
    <div data-role="page" id="index">
        <div data-theme="a" data-role="header">
            <h3>
                First Page
            </h3>
        </div>

        <div data-role="content">
            <a href="#" data-role="button" id="populate-button">Load JSON data</a>
            <select id="dpdLanguages"></select>
        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div>    
</body>
</html>   

jsop.php:

<?php
    echo '{"geographyInfo":[{"active":"true","countryCode":"US","countryName":"UNITED STATES","instanceID":"1","languageCode":"EN","languageName":"English","regionName":"North America"}],"reasonDetails":null,"size":"1","status":"true"}';    
?>

編集 :

JSONコードを変更できない場合は、これも機能します。

$(document).on('pagebeforeshow', '#index', function(){     
    $('#populate-button').on('click',function(e,data){     
        $('#dpdLanguages').empty()
        $.ajax({
            type: 'POST',
            url: "json.php",
            complete: function () { $.mobile.hidePageLoadingMsg(); },
            dataType: "json",
            contentType: 'application/json',
            data: "",
            success: function (output, textStatus, jqXHR) {
                if(typeof output.geographyInfo.length !== 'undefined') {                                      
                    $.each(output.geographyInfo, function (i, theItem) {
                        try {
                            $('<option>').attr('value',theItem.languageCode).html(theItem.languageName).appendTo("#dpdLanguages");
                        }
                        catch (error) {
                            alert(error);
                        }
                    });
                } else {
                    $('<option>').attr('value',output.geographyInfo['languageCode']).html(output.geographyInfo['languageName']).appendTo("#dpdLanguages");
                }
                $('#dpdLanguages').selectmenu();                            
            }
        });
    });        
});
于 2013-02-24T14:11:07.963 に答える