背景:私はキャリアセクションを作成するためにJSONデータを取得しようとしています。最初の$.getJSON()
リクエストでJSONデータをリクエストして取り込み、タイトルにデータを設定しました。次のステップでは、ポスティングID URLを取得し、2番目のリクエストを開いてDescription URLとApply URLを取得します。
問題: 2番目のリクエストは、オブジェクトがコンソールにプルされているのがわかりますが、何らかの理由でループにセクションまたはURLが設定されていないために機能します。私はまた、問題を特定するのが少し難しい、コンソールエラーを受け取っていません。非同期リクエストとは何か関係がありますか?私はAJAX、JSON、APIを初めて使う人です。
必要に応じてAPIの一部のマニュアル:この作業コードは、ちょうどこのラインvar jobs = jsonObj['content']
を変更する必要がhttps://dev.smartrecruiters.com/customer-api/posting-api/
// Create variable to append postings to
var postingsContainer = document.querySelector('div.job-container');
// Creates postings JSON request
$.getJSON('https://api.smartrecruiters.com/v1/companies/SynchronyGroup/postings', function(postings) {
// Check to see if data is being pulled
console.log(postings);
showJobs(postings);
});
// Function that pulls json data and populates careers section
function showJobs(jsonObj) {
// Variable that holds job postings json data
var jobs = jsonObj['content']
// Loop to create open position elements
for (var i = 0; i < jobs.length; i++) {
// Creates Column for job postings
var jobPosting = document.createElement('div')
jobPosting.setAttribute('class', 'col-12 col-md-6 col-lg-4 my-5 job-posting');
// Creates Job Title
var jobH5 = document.createElement('h5');
jobH5.textContent = jobs[i].name;
jobPosting.appendChild(jobH5);
postingsContainer.appendChild(jobPosting);
// Store job post IDs in var
var jobId = jobs[i].ref;
// Creates post 2nd ID JSON request
$.getJSON(jobId, function(data) {
// Check to see if data is being pulled
console.log(data);
showDetails(data);
});
}
}
//Function for posting description and apply url
function showDetails(data) {
// Loop to pull company description and apply url, then append to job posting element
for (var j = 0; j < data.length; j++) {
console.log("I work");
// Creates Company Desc. and Apply Link
var jobDetail = document.createElement('p');
var jobApply = document.createElement('a');
jobDetail.textContent = data[j].sections.companyDescription;
jobApply.setAttribute('href', data[j].applyUrl);
jobApply.setAttribute('class', 'btn-primary');
jobPosting.appendChild(jobDetail);
jobPosting.appendChild(jobApply);
}
}
'showDetails'メソッドの中に' jobPosting.appendChild(...) 'があります。この変数はこのスコープには存在しません。 – Draco18s
問題はこの行です。 'var jobs = jsonObj ['content']'。 'content'は配列ではないオブジェクトなので、' var jobs = jsonObj.content; 'に変更してください。 –