English 中文(简体)
改成正文法
原标题:convert textarea wrapped json data back to original

一些人建议总结MVC JsonReturn的产物,以 j的形式进行冰.。

这一部分是有意义的,但我如何在我的客户分类法中回击 j子?

客户杂草料应当看上去:

// Doesn t work since data is "<textarea>{"error":true,"msg":"foo"}</textarea>
success: function (data) {
                // strip textarea tags and convert data to json object
                if (data[ error ]) {
                    // data[ msg ]
                }
}

Thanks, Duffy

最佳回答

html()应当从内部内部撤出。

data = $.parseJSON(data.html());

the

问题回答

我理解这个问题。 我收到了<代码>的答复。 内容-类型:文本/html与下文一样。

<textarea>{"ContentEncoding":null,"ContentType":null,"Data":{"prop1":1},"JsonRequestBehavior":1,"MaxJsonLength":null,"RecursionLimit":null}</textarea>

阅读答复的客户法。

 function (xhr) 
 {                                      
        try{
             console.log(xhr.responseText);
             var originalData = $(xhr.responseText);
             var jsonResponse = $.parseJSON(originalData.html()); 
             var propValResult = jsonResponse.Data.prop1;                       
           }
        catch (e) 
        {                          

        };
 }

下面是服务器边代码,其中我提出了总结性反应。

ASP.NET MVC Customs Result

public class JsonTextWrappedResult : JsonResult
{

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        if ((JsonRequestBehavior == JsonRequestBehavior.DenyGet)
            && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException("Get Not Allowed");
        }

        var response = context.HttpContext.Response;
        response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }

        if (Data != null)
        {
            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string results = "<textarea>" + serializer.Serialize(Data) + "</textarea>";
            response.Write(results);
        }
    }
}

主计长办公室

public JsonTextWrappedResult PracticeInfoFormUpload(HttpPostedFileBase myfile, FormCollection formCollection)
    {


            var data = this.Json(
                            new
                            {
                                prop1 = 1
                            });
            var result = new JsonTextWrappedResult { Data = data, ContentType = "text/html" };
            return result;

    }




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签