gpt4 book ai didi

javascript - 通过AJAX(ASP.NET MVC)将json结果附加到div

转载 作者:行者123 更新时间:2023-12-01 05:21:23 24 4
gpt4 key购买 nike

我有一个有效的 AJAX 调用并返回 JSON

这是 AJAX 调用

<script>
$('#display').click(function () {
var vacancyId = $("#vacancy").val();
var model = {
vacancyId: vacancyId
};

$.ajax({
url: '@Url.Action("QuestionBlocks", "Questions")',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(model),
type: 'POST',
dataType: 'json',
processData: false,
success: function (data) {
$(".list").append('<div>' + data.Question1 + '</div>');

}
});
});

这是服务器端

  [HttpPost]
public ActionResult QuestionBlocks(int vacancyId)
{
var items = db.QuestionBlocks
.Where(x => x.Interview.VacancyId == vacancyId)
.Select(x => new
{
ID = x.Block_ID.ToString(),
Question1 = x.Question1,
Question2 = x.Question2,
Question3 = x.Question3,
Question4 = x.Question4,
Question5 = x.Question5,
Question6 = x.Question6,
Question7 = x.Question7,
Question8 = x.Question8,
Question9 = x.Question9,
Question10 = x.Question10,

})
.ToList();
return Json(items, JsonRequestBehavior.AllowGet);
}

它返回这样的数据

{ID: "1087", Question1: "Расскажите о себе", Question2: "Tell about you",…}

我的问题是 - $(".list").append('<div>' + data.Question1 + '</div>');运行良好,但显示 undefined

为什么会这样?

最佳答案

看起来data里面有一个数组,所以尝试这样的成功代码:

$.ajax({
url: '@Url.Action("QuestionBlocks", "Questions")',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(model),
type: 'POST',
dataType: 'json',
processData: false,
success: function (data) {
var question1 = data[0];
$(".list").append('<div>' + question1.Question1 + '</div>');

}
});

关于javascript - 通过AJAX(ASP.NET MVC)将json结果附加到div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43201930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com