开发者

convert textarea wrapped json data back to original

开发者 https://www.devze.com 2023-01-23 12:17 出处:网络
Lots of people recommend to wrap the MVC JsonReturn result in a textarea to play nicely with jquery forms etc.

Lots of people recommend to wrap the MVC JsonReturn result in a textarea to play nicely with jquery forms etc.

That part makes sense but how do I get the json object back in my client jquery code?

The client jquery plugin should look something like this:

// Doesn't work since data is "<textarea>{"error":true,"msg":"foo"}</textarea>
success: function (data) {
                // strip texta开发者_开发知识库rea tags and convert data to json object
                if (data['error']) {
                    // data['msg']
                }
}

Thanks, Duffy


.html() should pull out the innerhtml so

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

should do the trick


I understand this question is answered. I had a response with Content-Type:text/html like the one below.

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

Client Code for reading the response.

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

        };
 }

Following is the server side code where I create the wrapped response.

ASP.NET MVC Custom 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);
        }
    }
}

Controller Code Example

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;

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消