【问题标题】:Trouble with AJAX, JS, and Select2AJAX、JS 和 Select2 的问题
【发布时间】:2013-01-20 15:30:37
【问题描述】:

如果我做了一些愚蠢的事情,我深表歉意,但我是 Select2 和 Ajax 的新手,并且在尝试完成这项工作时遇到了很多麻烦。我正在尝试创建自己的 json 文件(link removed)以与我正在构建的表单(link removed)一起使用。在 Chrome 中使用 Firefox Lite,我能够从我的服务器获得响应(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>

【问题讨论】:

    标签: javascript ajax jquery jquery-select2


    【解决方案1】:

    您将 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};
             }
    

    【讨论】:

    • 我实际上是想使用 json(不是 jsonp)。当我更新我的代码以反映这一点以及您的第二次修复时,现在填充了选择,但所有项目都说“未定义”。有什么想法吗?
    • 两个问题: 1. 你确定你没有在 JavaScript 控制台中得到 CORS 错误吗(类似于 ..not allowed by Access-Control-Allow-Origin.) .任何连接到 willwelch.net 的不是来自该域的人都会收到该错误。 2. 没有formatResultformatSelection 行可以试试吗?这些函数似乎特定于 rottentomatoes API。
    • 谢谢!!!当我应该调用movie.text时,我正在调用movie.title。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多