Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 没有收到JSONP回调_Asp.net Mvc_Jquery_Asp.net Mvc 4_Jsonp - Fatal编程技术网

Asp.net mvc 没有收到JSONP回调

Asp.net mvc 没有收到JSONP回调,asp.net-mvc,jquery,asp.net-mvc-4,jsonp,Asp.net Mvc,Jquery,Asp.net Mvc 4,Jsonp,下面是ASP.NET MVC和JSONP博客文章的示例代码/教程: 我已经获取了代码示例,并对其进行了修改以供自己使用 当我点击页面时,它会触发控制器的操作,但是$.getJSON(调用,函数(rsp)…根本没有触发 控制器动作 [JsonpFilter] public JsonpResult GetMyObjects(int id) { List<MyObject> list = MyDAO.GetMyObjects(id); return new JsonpRe

下面是ASP.NET MVC和JSONP博客文章的示例代码/教程:

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

当我点击页面时,它会触发控制器的操作,但是
$.getJSON(调用,函数(rsp)…
根本没有触发

控制器动作

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

    return new JsonpResult
        {
            Data = list,
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
}
[JsonpFilter]
public JsonpResult GetMyObjects(int-id)
{
List=MyDAO.GetMyObjects(id);
返回新的JsonpResult
{
数据=列表,
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>

变量url=”http://localhost/MySite.ContentDelivery/MyController/GetMyObjects/?";
函数getObjects(){
//
//构建URL
//
调试器;
var call=url+“id=48&jsoncallback=?”;
//
//进行ajax调用
//
$.getJSON(调用、函数(rsp){
调试器;
警报(rsp);
如果(rsp.stat!=“正常”){
//
//出了点问题!
//
$(“#myDiv”)。追加(
“哎呀!没用!”+
“这太尴尬了!这是系统必须做的”+
“说说这个-”+rsp.message+”);
}
否则{
//
//构建html
//
var html=“”;
$.each(rsp.list.myObject,函数(){
var obj=这个;
html+=“替换:

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

您正在使用的自定义
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);
}
[JsonpFilter]
公共操作结果GetMyObjects(int id)
{
List=MyDAO.GetMyObjects(id);
返回Json(列表,JsonRequestBehavior.AllowGet);
}
或其他:

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

    return new JsonpResult
    {
        Data = list,
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}
public ActionResult GetMyObjects(int-id)
{
List=MyDAO.GetMyObjects(id);
返回新的JsonpResult
{
数据=列表,
JsonRequestBehavior=JsonRequestBehavior.AllowGet
};
}

但不要将两者混用。

谢谢,这对我很有帮助。我现在应该为以下问题创建一个新问题吗?
rsp
在执行
.getJson()
时是空的,即它不包含列表。是的,你可以创建一个新问题。但是不要忘记显示重要的代码(即,您的控制器操作返回的
MyObject
模型和
MyDAO.GetMyObjects
方法实现)。感谢Darin,我刚刚用实际的原始代码创建了新问题,没有混淆的属性等:
public ActionResult GetMyObjects(int id)
{
    List<MyObject> list = MyDAO.GetMyObjects(id);

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