我有AJAX調用的作品,並返回我JSON追加JSON結果通過AJAX(ASP.NET MVC)的div
這裏是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
爲什麼會這樣?
您可以打印成功FUNC數據?請添加'console.log(數據)! !!(「。list」)。append ...'。 –