1

最近,我被告知我需要使用对相应 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"
        }
    ]
}
4

3 回答 3

1

我为在个人项目上启用跨站点执行所做的就是这样做

    if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') === FALSE)
        die('You shouldn\'t be here');

    header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    header('Access-Control-Max-Age: 1000');
    header('Access-Control-Allow-Headers: Content-Type');

如果你想让它更安全一点,你可以这样做

    if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') === FALSE)
        die('You shouldn\'t be here');

switch($_SERVER['HTTP_ORIGIN']){
case 'domain.com':
case 'whatever.com':
        header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
        header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
        header('Access-Control-Max-Age: 1000');
        header('Access-Control-Allow-Headers: Content-Type');
}

希望这会有所帮助,我花了很长时间才弄清楚哈哈。

于 2012-05-18T17:06:21.900 回答
0
$.each(data.records, function() {
  console.log(this.Name);
});
于 2012-05-18T16:38:16.733 回答
0

这听起来像是一个跨域问题。除非其他服务器授予您访问权限,否则您无法从其他域访问 JSON 数据。

您可以选择使用 JSONP,但同样,仅当远程站点支持它时。

于 2012-05-18T16:41:42.357 回答