0
我下面的ASP.NET MVC和JSONP博客文章示例代碼/教程:http://blogorama.nerdworks.in/entry-EnablingJSONPcallsonASPNETMVC.aspx沒有收到JSONP回調
我所採取的代碼示例,並修改爲我自己的消費。
當我打開頁面時,它觸發了我的控制器的操作,但$.getJSON(call, function (rsp)..
根本沒有觸發。
控制器動作
[JsonpFilter]
public JsonpResult GetMyObjects(int id)
{
List<MyObject> list = MyDAO.GetMyObjects(id);
return new JsonpResult
{
Data = list,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
HTML頁面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
var url = "http://localhost/MySite.ContentDelivery/MyController/GetMyObjects/?";
function getObjects() {
//
// build the URL
//
debugger;
var call = url + "id=48&jsoncallback=?";
//
// make the ajax call
//
$.getJSON(call, function (rsp) {
debugger;
alert(rsp);
if (rsp.stat != "ok") {
//
// something went wrong!
//
$("#myDiv").append(
"<label style=\"background-color:red;color:white;padding: 25px;\">Whoops! It didn't work!" +
" This is embarrassing! Here's what the system had to " +
" say about this - " + rsp.message + "</label>");
}
else {
//
// build the html
//
var html = "";
$.each(rsp.list.myObject, function() {
var obj = this;
html += "<span" + obj.Name + "</span> <br />";
});
//
// append this to the div
//
$("#myDiv").append(html);
}
});
}
//
// get the offers
//
$(document).ready(function() {
alert('go..');
$(getOobjects);
});
</script>
<div id="myDiv"></div>
</body>
</html>
TL;博士爲什麼我getJson()
不點火,而我的getObjects()
火災和執行MVC控制器動作。
謝謝,這對我有幫助。我現在是否應該爲以下問題創建一個新問題 - 當'.getJson()'執行時,'rsp'是空的,即它不包含列表。 – Seany84 2013-02-24 16:49:01
是的,你可以創建一個新的問題。但不要忘記顯示重要的代碼(即您的contorller操作正在返回的'MyObject'模型和'MyDAO.GetMyObjects'方法實現)。 – 2013-02-24 16:52:03
感謝達林,我剛剛創建了新的問題與實際的原始代碼,沒有混淆的屬性等:http://stackoverflow.com/questions/15054200/jsonp-callback-response-data-is-null – Seany84 2013-02-24 17:10:07