Javascript 需要附加嵌套jQuery AJAX调用的帮助吗
我似乎无法从spoonacular使用的API将此URL附加到HTML中。控制台日志显示有东西,但它不想附加到html中。我有第一个ajax调用,它调用食物并提供一个需要在另一个ajax调用中传递的图像和ID对象,以获取食谱。当尝试进行第二次调用时,一切似乎都进行得很顺利,除了尝试将标记附加到应该容纳它的html div中。第一年的编码和任何建议都会非常有用Javascript 需要附加嵌套jQuery AJAX调用的帮助吗,javascript,jquery,ajax,Javascript,Jquery,Ajax,我似乎无法从spoonacular使用的API将此URL附加到HTML中。控制台日志显示有东西,但它不想附加到html中。我有第一个ajax调用,它调用食物并提供一个需要在另一个ajax调用中传递的图像和ID对象,以获取食谱。当尝试进行第二次调用时,一切似乎都进行得很顺利,除了尝试将标记附加到应该容纳它的html div中。第一年的编码和任何建议都会非常有用 function getSource(id) { let queryURL = "https://ap
function getSource(id) {
let queryURL = "https://api.spoonacular.com/recipes/complexSearch?apiKey=233e29c645cf4eaca90809d7a4696969&query="+id;
$.ajax ({
url: queryURL,
method: "GET" })
.then(function(response) {
console.log(response);
// JSON.stringify(response)
createNutritionBlock(response);
;
})}
//create function to write nutrition block and call write nutrition function in ajax
function createNutritionBlock(response){
var foodImageDiv = $("<div class='food'>");
var actualFoodImage = $("<img>").attr("src", response.results[0].image);
console.log(actualFoodImage);
foodImageDiv.append(actualFoodImage);
console.log(foodImageDiv)
$("#foodGoesHere").html(foodImageDiv);
getRecipe()
function getRecipe() {
var spoonFoodId = response.results[0].id
console.log(spoonFoodId);
var queryURLForRecipes = "https://api.spoonacular.com/recipes/"+spoonFoodId+"/similar?apiKey=233e29c645cf4eaca90809d7a4696969"
$.ajax ({
url: queryURLForRecipes,
method: "GET" })
.then(function(recipe) {
console.log(recipe);
var recipeDiv = $("<div class='recipes'>")
foodLink = $("<a>").attr("href", recipe.sourceURL)
console.log(foodLink);
//NEED HELP RIGHT HERE //
recipeDiv.append(foodLink)
$("#recipeGoesHere").append(recipeDiv);
})
}
}
// NEED HELP END //
$("#searchButton").on("click", function(event) {
// Preventing the button from trying to submit the form
event.preventDefault();
// Storing the food name
var inputFood = $("#search").val().trim();
// Running the food function(passing in the food as an argument)
getSource(inputFood);
});
});
函数getSource(id){
让queryURL=”https://api.spoonacular.com/recipes/complexSearch?apiKey=233e29c645cf4eaca90809d7a4696969&query=“+id;
$.ajax({
url:queryURL,
方法:“获取”})
.然后(功能(响应){
控制台日志(响应);
//stringify(响应)
createNutritionBlock(响应);
;
})}
//创建函数来编写营养块,并在ajax中调用write nutrition函数
函数createNutritionBlock(响应){
var foodImageDiv=$(“”);
变量actualFoodImage=$(“”)
foodLink=$(“”).attr(“href”,recipe.sourceURL)
console.log(foodLink);
//这里需要帮助吗//
recipeDiv.append(foodLink)
$(“#recipeGoesHere”)。追加(recipeDiv);
})
}
}
//需要帮助结束吗//
$(“#搜索按钮”)。在(“单击”上,函数(事件){
//阻止按钮尝试提交表单
event.preventDefault();
//储存食物名称
var inputFood=$(“#搜索”).val().trim();
//运行food函数(作为参数传入food)
getSource(输入食品);
});
});
代码显示正常,但链接元素没有文本或其他内容。这是预期的吗?