ローカル マシンでホストされている単純な MVC Web API がありPersonnel
、JSON でオブジェクトを返すだけです。同じマシン上に、単純な html ページがあり、getJSON()
.
getJSON
応答が正常に返され、サーバーとクライアントが同じマシンであり、JSON が有効であるにもかかわらず、呼び出しは次のエラーで失敗します。
parsererror Error: jQuery110206749425150919706_1382127977754 was not called
コントローラーで…
// GET api/personnel
public List<Personnel> Get()
{
return GetPersonnel();
}
GetPersonnel()
Personnel
オブジェクトのリストを返すだけです
public class Personnel
{
public String FirstName { get; set; }
public String LastName { get; set; }
public String RoomNumber { get; set; }
}
JSON のみが返されるように Application_Start() に追加されました...
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
このAPIを呼び出すために使用しているjQueryを次に示します(同じボックスで)。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$.ajaxSetup({ async:false});
$(document).ready(function () {
$("button").click(function () {
var api = "http://localhost:13131/DirectoryServicePool/api/personnel?callback=?"; // this api doesn't work
//api = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?"; // this api does
$.getJSON(api, {
format: "json"
}, function (result) {
alert("GotJSON");
}).fail(function (XHR, status, error) {
alert(status + ' ' + error);
});
});
});
</script>
</head>
<body><button>Get JSON data</button></body>
</html>
フィドラーの応答(JSONは有効です)...
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 18 Oct 2013 20:08:48 GMT
Content-Length: 244
[{"FirstName":"1","LastName":"5","RoomNumber":"6"},{"FirstName":"2","LastName":"5","RoomNumber":"6"},{"FirstName":"3","LastName":"5","RoomNumber":"6"},{"FirstName":"4","LastName":"5","RoomNumber":"6"}]
前もって感謝します。