请参阅下面的代码从我的MVC视图:AJAX返回undefined
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
function GetMessage() {
$.ajax({
type: "GET",
url: "http://localhost/webapi/api/Values/",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess(),
async: false,
failure: function (response) {
alert('there was an error creating the disposal decision records')
}
});
function OnSuccess() {
return function (response) {
alert(response.d);
}
}
}
GetMessage();
</script>
,并从代码:http://localhost/webapi/api/Values/:
public Person Get()
{
Person p = new Person();
p.id = 1;
p.name = "Bert";
p.age = 31
return p;
}
在成功的事件处理程序;提示未定义。为什么?
什么是'response.d'? – Nic
@Nic,请参阅:http://stackoverflow.com/questions/4215386/jquery-ajax-json-response-returns-key-d – w0051977
尝试'console.log(response)'并查看它包含的内容。警报对调试不太好。 – JJJ