如果没有收到JSONP回调 [英] Not receiving JSONP callback

查看:209
本文介绍了如果没有收到JSONP回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的ASP.NET MVC和JSONP博客文章示例code /教程:的 http://blogorama.nerdworks.in/entry-EnablingJSONPcallsonASPNETMVC.aspx

I am following the sample code/tutorial for ASP.NET MVC and JSONP blog post: http://blogorama.nerdworks.in/entry-EnablingJSONPcallsonASPNETMVC.aspx

我已经采取了code样品,并已修改为我自己的消费。

I have taken the code sample and have modified it for my own consumption.

当我打的页面,它激发了我的控制器的动作,但 $。的getJSON(调用函数(RSP).. 不点火的。

When I hit the page, it fires off my controller's action but the $.getJSON(call, function (rsp).. is not firing at all.

控制器动作

[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控制器动作。

tl;dr why is my getJson() not firing while my getObjects() fires and executes the MVC controller action.

推荐答案

替换为:

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

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

自定义的Json presult 您使用的是依赖于一个名为回调,而不是一个查询字符串参数 jsoncallback

The custom JsonpResult you are using relies on a query string parameter called callback and not jsoncallback:

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

另外你装饰了 [JsonpFilter] 属性您的控制器动作,返回一个的Json presult 。正如在文章中解释,你一定看过你应该选择一个:

Also you have decorated your controller action with a [JsonpFilter] attribute and returning a JsonpResult. As explained in the article you must have read you should choose one:

[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
    };
}

但不要混用两种。

but do not mix the two.

这篇关于如果没有收到JSONP回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆