服务器端代码
public ActionResult GetNewUploadCourseIds() { string time = "0"; var result = new Result<NewUpload>() { Data = new NewUpload() }; result.Data.Ids = courseBLL.GetNewUploadCourseIdsByTime(time); result.Data.Time = DateTime.Now; result.Data.UpdateCount = courseBLL.GetNewUpdateCourseCountByTime(time); return new ContentResult() { Content = "myCallBack(" + JsonConvert.SerializeObject(result) + ")", ContentType = "text/html" }; }
//ContentType一定要是text/html类型。
返回值是
然后是jQuery代码:
<script type="text/javascript"> $(function () { $("#btn").click(function () { $.ajax({ url: "http://localhost:50049/service/GetNewUploadCourseIds?time=123", dataType: "jsonp", // jsonp: "", // jsonpCallback: "myCallBack", //需要的回调函数 success: function (data) { alert("success"); }, error: function () { alert("fail"); } }); }); }); function myCallBack(result) { alert(result.Data.UpdateCount); }; </script>
执行结果:
弹出alert框两次,分别是回调函数和"sucess"
、