我似乎无法将 JSON 值插入到标记中的输入元素中。有什么想法吗?我确实验证了 .ajax 调用是否返回了正确的 JSON。它可能很简单,但我似乎无法让它工作。谢谢!
html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-2.0.3.min.js"></script>
</head>
<body>
<div>
<form>
<fieldset>
<legend>The Person</legend>
<label>Name: </label>
<input id="Name"/>
<label>Age: </label>
<input id="Age" />
</fieldset>
</form>
</div>
<script type="text/javascript" src="~/Scripts/Custom.js"></script>
</body>
</html>
javascript
$(document).ready(function () {
$.ajax({
url: '/Home/GetPerson',
type: 'GET',
success: function (result) {
alert(JSON.stringify(result));
$('#Name').val(result[0].FirstName);
}
});
});
JSON.stringify 返回
{"FirstName": "Steve", "LastName": "Smith"}