ASP.NET MVC 3 JSONP: Does this work with JsonValueProviderFactory? -


phil haack has excellent blog post on how use json, data binding, , data validation.

enter browser's "same origin policy security restriction." , jsonp use $.getjson() retrieve content.

is there built in mvc 3 way this, or need follow advice of posts this? can post content? ask because colleague implemented jsonpfilterattribute among other things make work. it's preferred avoid if exists in mvc 3.

edit:

summary: works exception of accessing post variable, i.e., how access post variable in context? (comment marking in last section of code)

i elected use format call server:

$.ajax({     type: "get",     url: "getmydatajsonp",     data: {},     contenttype: "application/json; charset=utf-8",     datatype: "jsonp",     jsonpcallback: "randomfunctionname" }); 

which produces response:

randomfunctionname([{"firstfield":"111","secondfield":"222"}]); 

and works if use get. however, still cannot work post. here's original code posted nathan bridgewater here. line doesn't find post data:

context.httpcontext.request["callback"]; 

either should accessing form in way, or mvc data validators stripping out post variables.

how should context.httpcontext.request["callback"]; written access post variable or mvc stripping out these values reason?

namespace system.web.mvc {   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"];              // line need alter find form variable:              if (string.isnullorempty(this.jsoncallback))                 this.jsoncallback = context.httpcontext.request["callback"];              if (string.isnullorempty(this.jsoncallback))                 throw new argumentnullexception(                     "jsoncallback required jsonp response.");              httpresponsebase response = context.httpcontext.response;              if (!string.isnullorempty(contenttype))                response.contenttype = contenttype;             else                response.contenttype = "application/json; charset=utf-8";              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 controller 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;         }     } } 

as far receiving json string , binding model concerned jsonvalueproviderfactory job out of box in asp.net mvc 3. there nothing built-in outputting jsonp. write custom jsonpresult:

public class jsonpresult : jsonresult {     public override void executeresult(controllercontext context)     {         if (context == null)         {             throw new argumentnullexception("context");         }         var request = context.httpcontext.request;         var response = context.httpcontext.response;         string jsoncallback = (context.routedata.values["jsoncallback"] string) ?? request["jsoncallback"];         if (!string.isnullorempty(jsoncallback))         {             if (string.isnullorempty(base.contenttype))             {                 base.contenttype = "application/x-javascript";             }             response.write(string.format("{0}(", jsoncallback));         }         base.executeresult(context);         if (!string.isnullorempty(jsoncallback))         {             response.write(")");         }     } } 

and in controller action:

public actionresult foo() {     return new jsonpresult     {         data = new { prop1 = "value1", prop2 = "value2" },         jsonrequestbehavior = jsonrequestbehavior.allowget     }; } 

which consumed domain $.getjson():

$.getjson('http://domain.com/home/foo?jsoncallback=?', function(data) {     alert(data.prop1); }); 

Comments

Popular posts from this blog

sql server - python to mssql encoding problem -

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -