我无法获取 json 对象。
我的对象是:
public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
}
人员控制:
public ActionResult Index()
{
return View();
}
// GET: /Persons/GetPerson
//[AcceptVerbs(HttpVerbs.Get)]
//[OutputCache(Duration = 0, VaryByParam = "*")]
public JsonResult GetPerson()
{
Person p = new Person { firstName = "Jonas", lastName = "Antanaitis" };
return Json(p, JsonRequestBehavior.AllowGet);
}
我的索引视图:
@Scripts.Render("~/bundles/knockout")
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
<div id="dispJson"></div>
<script type="text/javascript">
var data = $.getJSON(".../GetPerson", function (result) {
//state = result.readyState;
firstName: result.firsName;
lastName: result.lastName;
})
.error(function () { alert("error"); });
function viewModel() {
ko.mapping.fromJS(data);
//state = data.readyState;
//firstName = data.firstName;
//lastName = data.lastName;
};
document.write(JSON.stringify(data)); //this line prints "{"readyState":1}"
ko.applyBindings(new viewModel());
</script>
当我在浏览器中转到“.../GetPerson”时,会显示: {"firstName":"Jonas","lastName":"Antanaitis"}
,但是当尝试在视图中获取数据巫婆 javascript - 我没有得到这些数据。
我做错了什么,为什么我无法获取数据? 注释代码的地方-我尝试了这种方法..但没有任何帮助。
我试过:
*$.ajax({
type: "GET",
cache: false,
url: ".../GetPerson",
}).done(function (msg) {
alert("Data Saved: " + msg.readyState + " " + msg.firstName + " " + msg.lastName);
});*
然后警报框显示:“ Data Saved: undefined Jonas Antanaitis ”