1

愚かなことをしていることをお詫びしますが、私はSelect2とAjaxを初めて使用し、この作業を行うのに非常に時間がかかっています。作成中のフォーム(リンクが削除されました)で使用する独自のjsonファイル(リンクが削除されました)を作成しようとしています。ChromeでFirefoxLiteを使用すると、サーバーから応答を受け取ることができます(200 OK)が、選択ボックスに検索結果が表示されません。私のjsonファイルはhttp://jsonformatter.curiousconcept.comを使用して正しく検証されているようです。あなたもそれを見ることができます

何か考えやヒントはありますか?

json.phpソース:

<?
    $myArray = array(
            array( "id" => "id1", "text" => "title1" ),
            array( "id" => "id2", "text" => "title2" )
            );

    echo json_encode($myArray); 

?>

select.phpソース:

<html>
<head>
    <link href="select2/select2.css" rel="stylesheet"/>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="select2/select2.js"></script>
    <script>
        function movieFormatResult(movie) {
            var markup = "<table class='movie-result'><tr>";
            markup += "<td class='movie-info'><div class='movie-title'>" + movie.title + "</div>";
            markup += "</td></tr></table>"
            return markup;
        }

        function movieFormatSelection(movie) {
            return movie.title;
        }

    </script>
    <script id="script_e6">
        $(document).ready(function() {
            $("#e6").select2({
                placeholder: "Search for a movie",
                minimumInputLength: 1,
                ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
                    url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
                    dataType: 'jsonp',
                    data: function (term, page) {
                        return {
                            q: term, // search term
                            page_limit: 10,
                            apikey: "my-api-key" // please do not use so this example keeps working
                        };
                    },
                    results: function (data, page) { // parse the results into the format expected by Select2.
                        // since we are using custom formatting functions we do not need to alter remote JSON data
                        return {results: data.movies};
                    }
                },
                formatResult: movieFormatResult, // omitted for brevity, see the source of this page
                formatSelection: movieFormatSelection,  // omitted for brevity, see the source of this page
                dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
            });
        });
    </script>
    <script id="script_e6a">
        $(document).ready(function() {
            $("#e6a").select2({
                placeholder: "Search for a movie",
                minimumInputLength: 1,
                ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
                    url: "http://willwelch.net/play/nhs/pstudents/json.php",
                    dataType: 'jsonp',
                    data: function () {
                        return;
                    },
                    results: function () { // parse the results into the format expected by Select2.
                        return {results: data};
                    }
                },
                formatResult: movieFormatResult, // omitted for brevity, see the source of this page
                formatSelection: movieFormatSelection,  // omitted for brevity, see the source of this page
                dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
            });
        });
    </script>    
</head>

<body style="height:500px;">
    <p>original example (works)</p>
    <input type="hidden" class="bigdrop" id="e6" style="width: 600px; display: none;"><br><br>
    <p>my version (does not work)</p>
    <input type="hidden" class="bigdrop" id="e6a" style="width: 600px; display: none;">
</body>

</html>
4

1 に答える 1

2

URL(willwelch.net)をjsonp呼び出しとして呼び出しています。PHP では、ajax 呼び出しによって渡されたパラメーターをキャプチャcallbackし、関数名 = コールバック パラメーターの内容を使用して、関数呼び出しで結果をラップする必要があります。

したがって、php への呼び出しは次のようになります。

 http://willwelch.net/play/nhs/pstudents/json.php?callback=myfunc

そして、返すべきものは次のとおりです。

 myfunc([{"id":"id1","text":"title1"},{"id":"id2","text":"title2"}])

ajax 呼び出しにより、コールバック パラメータが自動的に追加されることに注意してください。

編集

また、入力パラメーターとしてreturn追加するようにメソッドを変更する必要があります。data

         results: function (data) { // parse the results into the format expected by Select2.
               return {results: data};
         }
于 2013-01-20T17:38:47.933 に答える