没有收到JSONP回调

时间:2013-02-24 16:18:49

标签: asp.net-mvc jquery asp.net-mvc-4 jsonp

我正在关注ASP.NET MVC和JSONP博客文章的示例代码/教程:http://blogorama.nerdworks.in/entry-EnablingJSONPcallsonASPNETMVC.aspx

我已经采用了代码示例并对其进行了修改以供我自己使用。

当我点击页面时,它会触发我的控制器操作,但$.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; dr 为什么我的getJson()getObjects()触发并执行MVC控制器操作时未触发。

1 个答案:

答案 0 :(得分:1)

替换:

var call = url + "id=48&jsoncallback=?";

使用:

var call = url + "id=48&callback=?";

您使用的自定义JsonpResult依赖于名为callback而非jsoncallback的查询字符串参数:

Callback = context.HttpContext.Request.QueryString["callback"];

您还使用[JsonpFilter]属性修饰了控制器操作并返回JsonpResult。正如文章中所解释的,你必须阅读,你应该选择一个:

[JsonpFilter]
public ActionResult GetMyObjects(int id)
{
    List<MyObject> list = MyDAO.GetMyObjects(id);

    return Json(list, JsonRequestBehavior.AllowGet);
}

或其他:

public ActionResult GetMyObjects(int id)
{
    List<MyObject> list = MyDAO.GetMyObjects(id);

    return new JsonpResult
    {
        Data = list,
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}

但不要混淆两者。

相关问题