最近,我被告知我需要使用对相应 State 的查询从远程 Sales Force Counties 对象中读取,然后动态创建一个 select 语句这些值。我的问题是:虽然我知道当我在浏览器中手动使用查询时,我会得到 JSON 格式的数据,但是当我尝试解析县名时,我会在整个迭代中得到一个空值。这是我的代码。感谢您提供的任何建议/帮助:
<script language="JavaScript">
function getCounties(chosenState){
var url = "https://api.url=1&action=query/?q=SELECT Name FROM US_Counties__cWhere State_Name__c =";
url = url + "'"+chosenState+"'";
alert(url);
$.getJSON(url,
function(data) {
console.log(data)
var options = '';
$.each(data.records[0], function(i,item){
options += '<option value="' + item.attributes.url + '">' + item.attributes.Name + '</option>';
});
$("select#counties").html(options);
});
}
</script>
这是数据的一部分:
{
"totalSize": 36,
"done": true,
"records": [
{
"attributes": {
"type": "US_Counties__c",
"url": "/services/data/v20.0/sobjects/US_Counties__c/a1FR00000034BJLMA2"
},
"Name": "Baker"
},
{
"attributes": {
"type": "US_Counties__c",
"url": "/services/data/v20.0/sobjects/US_Counties__c/a1FR00000034BJMMA2"
},
"Name": "Benton"
},
{
"attributes": {
"type": "US_Counties__c",
"url": "/services/data/v20.0/sobjects/US_Counties__c/a1FR00000034BJNMA2"
},
"Name": "Clackamas"
},
{
"attributes": {
"type": "US_Counties__c",
"url": "/services/data/v20.0/sobjects/US_Counties__c/a1FR00000034BJOMA2"
},
"Name": "Clatsop"
},
{
"attributes": {
"type": "US_Counties__c",
"url": "/services/data/v20.0/sobjects/US_Counties__c/a1FR00000034BJPMA2"
},
"Name": "Columbia"
},
{
"attributes": {
"type": "US_Counties__c",
"url": "/services/data/v20.0/sobjects/US_Counties__c/a1FR00000034BJQMA2"
},
"Name": "Coos"
},
{
"attributes": {
"type": "US_Counties__c",
"url": "/services/data/v20.0/sobjects/US_Counties__c/a1FR00000034BJRMA2"
},
"Name": "Crook"
},
{
"attributes": {
"type": "US_Counties__c",
"url": "/services/data/v20.0/sobjects/US_Counties__c/a1FR00000034BJSMA2"
},
"Name": "Curry"
},
{
"attributes": {
"type": "US_Counties__c",
"url": "/services/data/v20.0/sobjects/US_Counties__c/a1FR00000034BJTMA2"
},
"Name": "Deschutes"
}
]
}