Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
';ObjectContent`1';类型未能序列化内容类型';的响应正文;application/json';_Json_Asp.net Mvc_Asp.net Web Api_Asp.net Web Api2_Appharbor - Fatal编程技术网

';ObjectContent`1';类型未能序列化内容类型';的响应正文;application/json';

';ObjectContent`1';类型未能序列化内容类型';的响应正文;application/json';,json,asp.net-mvc,asp.net-web-api,asp.net-web-api2,appharbor,Json,Asp.net Mvc,Asp.net Web Api,Asp.net Web Api2,Appharbor,我正在使用asp.NETMVC和WebApi。在localhost上一切正常,但在appharbor部署的网站上,我遇到异常ObjectContent 1 type未能序列化content type的响应体'application/json;每次调用webapi控制器时,charset=utf-8' 我的webapi函数是: public async Task<IHttpActionResult> GetMobileTrends() { var

我正在使用asp.NETMVC和WebApi。在localhost上一切正常,但在appharbor部署的网站上,我遇到异常
ObjectContent 1 type未能序列化content type的响应体'application/json;每次调用webapi控制器时,charset=utf-8'

我的webapi函数是:

public async Task<IHttpActionResult> GetMobileTrends()
        {
            var ret = (from ad in db.MobileAds
                      orderby ad.Ad.views
                      where ad.Ad.AdImages.Count > 0
                      select new
                      {
                          title = ad.Ad.title,
                          id = ad.Ad.Id,
                          //other attributes
                      });
            return Ok(ret);
        }
self.loadad = function () {
                url_address = '/api/Electronic/GetMobileTrends';
                $.ajax({
                    url: url_address,
                    dataType: "json",
                    contentType: "application/json",
                    cache: false,
                    type: 'GET',
                    success: function (data) {
                        var mappedads = $.map(data, function (item) { return new ad(item); });
                        self.showAds(mappedads);

                    },
                    error: function () {
                        toastr.error("Unable to load data. Please try again", "Error");
                    }
                });
            }
但它不起作用。然后我在
WebApiConfig.cs
中的
Register
函数末尾添加了以下行:

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
        }
var json = configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);
它仍然不起作用。我不想创建一个模型,因为我正在使用knockout。另外,如果我为每个webapi调用创建模型。将有30到40个模型(每个调用中检索的数据不同)

有没有办法在不创建模型的情况下解决此异常

更新: 我的问题不是重复的。 我的ajax功能是:

public async Task<IHttpActionResult> GetMobileTrends()
        {
            var ret = (from ad in db.MobileAds
                      orderby ad.Ad.views
                      where ad.Ad.AdImages.Count > 0
                      select new
                      {
                          title = ad.Ad.title,
                          id = ad.Ad.Id,
                          //other attributes
                      });
            return Ok(ret);
        }
self.loadad = function () {
                url_address = '/api/Electronic/GetMobileTrends';
                $.ajax({
                    url: url_address,
                    dataType: "json",
                    contentType: "application/json",
                    cache: false,
                    type: 'GET',
                    success: function (data) {
                        var mappedads = $.map(data, function (item) { return new ad(item); });
                        self.showAds(mappedads);

                    },
                    error: function () {
                        toastr.error("Unable to load data. Please try again", "Error");
                    }
                });
            }

有什么意见吗??如果我的问题不清楚,请告诉我。我的问题可能重复,但不重复。我已经在使用
application/json
(如回答1所述),并且我已经尝试添加行
config.Formatters.Clear();Add(新的JsonMediaTypeFormatter());config.maphttpAttribute路由()在寄存器功能中(如回答2中所述),但它不起作用。我也遇到了这个问题。在本地主机上一切正常,但在部署时则不然。如果是代码或模型问题,也会在本地发生。这也是最近的一个问题。我怀疑上游发生了变化,但我需要一些灵感去寻找。我遇到了同样的问题,并通过在api控制器中创建构造函数解决了这个问题,并将db.Configuration.ProxyCreationEnabled=false;在构造函数中。我在任何评论中都跟随了一条评论??如果我的问题不清楚,请告诉我。我的问题可能重复,但不重复。我已经在使用
application/json
(如回答1所述),并且我已经尝试添加行
config.Formatters.Clear();Add(新的JsonMediaTypeFormatter());config.maphttpAttribute路由()在寄存器功能中(如回答2中所述),但它不起作用。我也遇到了这个问题。在本地主机上一切正常,但在部署时则不然。如果是代码或模型问题,也会在本地发生。这也是最近的一个问题。我怀疑上游发生了变化,但我需要一些灵感去寻找。我遇到了同样的问题,并通过在api控制器中创建构造函数解决了这个问题,并将db.Configuration.ProxyCreationEnabled=false;在构造函数中。我关注了一条评论