服务器端代码

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类型。

返回值是

 

JsonP的简单demo

然后是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"

 

相关文章:

  • 2021-09-02
  • 2021-06-06
  • 2021-10-12
  • 2022-03-02
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-03
  • 2021-07-06
  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
  • 2021-05-31
  • 2022-03-08
相关资源
相似解决方案