无法弄清楚为啥我在我的 jsonp 序列化中得到重复数据
Posted
技术标签:
【中文标题】无法弄清楚为啥我在我的 jsonp 序列化中得到重复数据【英文标题】:Can't figure out why I'm getting duplicate data in my jsonp serialization无法弄清楚为什么我在我的 jsonp 序列化中得到重复数据 【发布时间】:2011-09-14 22:35:18 【问题描述】:我正在使用我在searching here on SO 中找到的自定义 JsonpResult 类。我已经通读了代码,并且了解它是如何工作的(至少我认为我是这样做的),但是……由于某种原因,当我序列化我的 Object
时,我得到了重复的字符串……为什么会这样?
这是自定义类
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* Content of this class was mostly derived from the original
* JsonResult class in the System.Web.Mvc 2.0 RTM Assembly. This
* has beeen slightly extended for use with JSONP calls.
*
* This software is subject to the Microsoft Public License (Ms-PL).
* A copy of the license can be found in the license.htm file included
* in this distribution.
*
* You must not remove this notice, or any other, from this software.
*
* ***************************************************************************/
namespace System.Web.Mvc
using System;
using System.Text;
using System.Web;
using System.Web.Mvc.Resources;
using System.Web.Script.Serialization;
public class JsonpResult : ActionResult
public JsonpResult()
public Encoding ContentEncoding get; set;
public string ContentType get; set;
public object Data get; set;
public string JsonCallback get; set;
public override void ExecuteResult(ControllerContext context)
if (context == null)
throw new ArgumentNullException("context");
this.JsonCallback = context.HttpContext.Request["jsoncallback"];
if (string.IsNullOrEmpty(this.JsonCallback))
this.JsonCallback = context.HttpContext.Request["callback"];
if (string.IsNullOrEmpty(this.JsonCallback))
throw new ArgumentNullException("JsonCallback required for JSONP response.");
HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
response.ContentType = ContentType;
else
response.ContentType = "application/javascript";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data != null)
JavaScriptSerializer serializer = new JavaScriptSerializer();
response.Write(string.Format("0(1);", this.JsonCallback, serializer.Serialize(Data)));
//extension methods for the controller to allow jsonp.
public static class ContollerExtensions
public static JsonpResult Jsonp(this Controller controller, object data)
JsonpResult result = new JsonpResult();
result.Data = data;
result.ExecuteResult(controller.ControllerContext);
return result;
这是我的控制器操作
public class VimeoController : Controller
//
// GET: /Vimeo/
public JsonpResult Read()
Models.ViewModels.JsonViewModel JsonResponse;
try
VimeoSharp.APIs.VimeoSimple VimeoSimple = new VimeoSharp.APIs.VimeoSimple();
// Tell VimeoSharp what channel to pull it's videos from (193328)
List<VimeoSharp.Video> VimeoList = VimeoSimple.ChannelVideos("193328");
// Create a viewmodel list of videos to be used.
// we're only using id and title for this bit.
List<Models.Pocos.Vimeo> videoList = new List<Models.Pocos.Vimeo>();
foreach (VimeoSharp.Video record in VimeoList)
videoList.Add(new Models.Pocos.Vimeo
id = 1, //Int32.Parse(record.ID),
title = "a" //(record.Title.Length > 26 ? record.Title.Substring(0, 25) + "..." : record.Title)
);
;
JsonResponse = new Models.ViewModels.JsonViewModel
results = videoList,
success = true
;
catch
JsonResponse = new Models.ViewModels.JsonViewModel
results = null,
success = false
;
// a failed response
return this.Jsonp(JsonResponse);
这是输出结果。
CALLBACK1001("results":["id":1,"title":"a","id":1,"title":"a","id":1, "title":"a","id":1,"title":"a","id":1,"title":"a","id":1,"title ":"a","id":1,"title":"a","id":1,"title":"a","id":1,"title": "a","id":1,"title":"a","id":1,"title":"a","id":1,"title":"a ","id":1,"title":"a","id":1,"title":"a"],"success":true);CALLBACK1001("results" :["id":1,"title":"a","id":1,"title":"a","id":1,"title":"a", "id":1,"title":"a","id":1,"title":"a","id":1,"title":"a"," id":1,"title":"a","id":1,"title":"a","id":1,"title":"a","id" :1,"title":"a","id":1,"title":"a","id":1,"title":"a","id":1 ,"title":"a","id":1,"title":"a"],"success":true);
为什么我的JsonResponse
被序列化了两次?
【问题讨论】:
【参考方案1】:问题出在你的扩展方法上:
public static class ContollerExtensions
public static JsonpResult Jsonp(this Controller controller, object data)
JsonpResult result = new JsonpResult();
result.Data = data;
//result.ExecuteResult(controller.ControllerContext); <-- Remove this !!!
return result;
注意我评论的那一行。您基本上是两次调用结果。在 ActionResult
上调用 ExecuteResult
方法是 ASP.NET MVC 框架的责任,而不是你的。
另外,如果您在 *** 上找到此代码,为什么它是 Copyright (c) Microsoft Corporation。保留所有权利。 :-)
【讨论】:
不知道为什么它是微软的版权。我认为我发现的帖子是从一个更大的项目中提取的。 @rockinthesixstring,别担心,这只是为了好玩 :-) 那么有什么好处呢?您是否设法通过删除违规行来实现这一目标? 是的,就是这样。谢谢。 我只需要先测试一下...我的 lappy 有点迟钝。以上是关于无法弄清楚为啥我在我的 jsonp 序列化中得到重复数据的主要内容,如果未能解决你的问题,请参考以下文章
无法弄清楚为啥我的字符、字数和行数适用于文件输入,但不适用于标准输入
无法弄清楚为啥 docker compose up 没有运行我的 React JS 应用程序