Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 选择2 ajax-won';t显示返回的json数据_Jquery_Json_Jquery Select2 - Fatal编程技术网

Jquery 选择2 ajax-won';t显示返回的json数据

Jquery 选择2 ajax-won';t显示返回的json数据,jquery,json,jquery-select2,Jquery,Json,Jquery Select2,下面是从我的coldfusion页面返回的json字符串的样子:[{“client”:“Asante”,“id”:12},{“client”:“兰开斯特市”,“id”:14},{“client”:“Massey Energy”,“id”:35},{“client”:“Northeast Utilities”,“id”:68},{“client”:“Washtenaw”,“id”:50}。Firebug声称一切正常,但select2插件中没有显示任何数据 有人知道问题出在哪里吗?它应该返回列名还是

下面是从我的coldfusion页面返回的json字符串的样子:
[{“client”:“Asante”,“id”:12},{“client”:“兰开斯特市”,“id”:14},{“client”:“Massey Energy”,“id”:35},{“client”:“Northeast Utilities”,“id”:68},{“client”:“Washtenaw”,“id”:50}
。Firebug声称一切正常,但select2插件中没有显示任何数据

有人知道问题出在哪里吗?它应该返回列名还是什么

选择2呼叫:

$(".select").select2({
    allowClear: true,
    blurOnChange: true,
    openOnEnter: false,
    ajax: {
        url: "/surveymanagement/admin/client.cfc",
        dataType: 'json',
        data: function (term, page) {
            return {
                method: "GetClientsByName",
                name: term
            };
        },
        results: function (data, page) {
            return { results: data };
        }
    }
});

您的数据必须采用
[{“text”:“Asante”,“id”:12},…]
格式,否则您需要传递
{results:data,text:'client'}
如果您的json字符串需要使用
“text”:“something”
以外的内容,则需要添加以下内容:使用
formatResults
来显示数据。以下是固定版本:

$(".select").select2({
    allowClear: true,
    blurOnChange: true,
    openOnEnter: false,
    ajax: {
        url: "/surveymanagement/admin/client.cfc",
        dataType: 'json',
        data: function (term, page) {
            return {
                method: "GetClientsByName",
                name: term
            };
        },
        results: function (data, page) {
            return { results: data };
        }
    },
    formatResult: function (data) {
        return "<div class='select2-user-result'>" + data.client + "</div>";
    },
    formatSelection: function (data) {
        return data.client;
    }
});
$(“.select”)。选择2({
allowClear:是的,
没错,
openOnEnter:false,
阿贾克斯:{
url:“/surveymanagement/admin/client.cfc”,
数据类型:“json”,
数据:功能(术语,第页){
返回{
方法:“GetClientsByName”,
姓名:任期
};
},
结果:功能(数据、页面){
返回{结果:数据};
}
},
formatResult:函数(数据){
返回“+data.client+”;
},
formatSelection:函数(数据){
返回数据.client;
}
});
否则,Arun是正确的,您只需要使用格式
[{“id”:1,“text”:“client”}]

是的,它太旧了:),但我今天需要它,并像这样解决它(使用Symfony2):



键“results”很重要

您的数据必须采用
[{“text”:“Asante”,“id”:12},…]格式
否则您需要传递
{results:data,text:'client'}
$opts = [];

foreach($items as $item)

    $opts['results'][] = ['text' => $item->getXyz(), 'id' => $sk->getId()];

return new JsonResponse($opts);