为什么我无法使用Ajax访问此变量的属性?

时间:2017-05-24 20:27:49

标签: javascript jquery ajax

我正在尝试制作ajax请求,以检查我是否可以访问自动生成的对象内的title属性,因此我无法知道其名称。所以这就是我所做的:

$.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=apple',
            success: function(data){
                //console.log(data['query']['pages']['856']['title']);
                for (const i in data['query']['pages']){
                    console.log(i['title']);
                }
            }
        })

然而它让我回归" undefined"作为回应。如果我只留下console.log(i)它会显示一切正确。已经尝试了i.title而且没有。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:2)

变量java.lang.Object是对象中的键,而不是对象本身。

如果您想访问i是其关键字的对象,您应该使用:

i

您的代码应如下所示:

data['query']['pages'][i]
  

请注意,更好地查看对象密钥的方法是使用$.ajax({ type: 'GET', dataType: 'json', url: 'https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=apple', success: function(data){ //console.log(data['query']['pages']['856']['title']); for (const i in data['query']['pages']){ console.log(data['query']['pages'][i]['title']); } } })

所以我会改用这段代码:

Object.keys(obj)

答案 1 :(得分:0)

您是否尝试退出i?您会看到i是一个字符串,因为for..in抓取了对象的,而不是值。要获取值,您需要使用原始对象上的键。



let obj = {
  a: 1,
  b: 2,
  c: 3
};

for (let key in obj) {
  console.log(`Key: ${key}`);
  console.log(`Value: ${obj[key]}`);
}




因此,您的代码的重写版本可能如下所示:

$.ajax({
  type: 'GET',
  dataType: 'json',
  url: 'https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=apple',
  success: function(data) {
    // Accessing properties with a . is the same as
    // using ['key']
    for (let i in data.query.pages){
      console.log(data.query.pages[i].title);
    }
  }
});

答案 2 :(得分:0)

您的console.log(data['query']['pages'][i]['title'])可以访问为:##### Config vars ##### $serverUri = 'http://localhost:8080/' # URI of your Jenkins server $jenkinsCli = 'C:\Program Files (x86)\Jenkins\war\WEB-INF\jenkins-cli.jar' # Path to jenkins-cli.jar on your machine $destFolder = 'C:\Jenkins Backup\' # Output folder (will be created if it doesn't exist) $destFile = 'jenkins-jobs.zip' # Output filename (will be overwritten if it exists) ######################## $work = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName()) New-Item -ItemType Directory -Force -Path $work | Out-Null # Suppress output noise echo "Created a temp working folder: $work" $jobs = (java -jar $jenkinsCli -s $serverUri list-jobs) echo "Found $($jobs.Length) existing jobs: [$jobs]" foreach ($j in $jobs) { $outfile = Join-Path $work "$j.xml" java -jar $jenkinsCli -s $serverUri get-job $j | Out-File $outfile } echo "Saved $($jobs.Length) jobs to temp XML files" New-Item -ItemType Directory -Force -Path $destFolder | Out-Null # Suppress output noise echo "Found (or created) $destFolder folder" $destPath = Join-Path $destFolder $destFile Get-ChildItem $work -Filter *.xml | Write-Zip -Level 9 -OutputPath $destPath -FlattenPaths | Out-Null # Suppress output noise echo "Copied $($jobs.Length) jobs to $destPath" Remove-Item $work -Recurse -Force echo "Removed temp working folder"

相关问题