您必须确保您的服务器返回有效的 JSON。这将是一个以字典样式的数组作为项目的平面数组(数字索引)。这是一个可能的样本;
<?php
//requestServerData.php
$data = array();
$item_one = array("name" => "state1", "id" => "s1");
$item_two = array("name" => "state2", "id" => "s2");
array_push($item_one);
array_push($item_two);
echo json_encode($data);
?>
把它和你提供的小提琴放在一起,通过requestServerData函数模拟XHR,就是这样:
// global handles for the store and combobox
var combo = null, store = null;
// simulaition of a call to PHP server, which returns a json encoded array
// e.g. "[{name:'state1', id:'s1'},{name:'state2', id:'s2'}]"
function requestServerData() {
var responseText = "[{name:'state1', id:'s1'},{name:'state2', id:'s2'}]";
var data = eval(responseText);
var newStore = new dojo.store.Memory({"data": data});
combo.set('store', newStore);
combo.set('value', data[0].name)
}
// since domReady is in the require, this code block is not run until page loads
require([
"dojo/store/Memory", "dijit/form/ComboBox", "dojo/domReady!"
], function(Memory, ComboBox){
store = new Memory({
data: [
{name:"Loading", id:"LOAD"}
]
});
combo = new ComboBox({
id: "stateSelect",
name: "state",
value: "Loading",
store: store,
searchAttr: "name"
}, "stateSelect");
combo.startup();
requestServerData()
});
你的小提琴,更新:https://jsfiddle.net/fuanzcps/3/