How to use jsonp and mvc to cross domains when we control both domains

1 comments

link here:

http://www.codeguru.com/csharp/.net/net_asp/using-jsonp-in-asp.net-mvc.htm


		public class ETAInfo {
			public DateTime? PlanningDate{get;set;}
			public bool IsTentativeDate{get;set;}
			public String PlanningDateText{get;set;}
		}

		[HttpGet]
		public JsonpResult GetETA(string Warehouse_WHLO,string ItemNumber_ITNO, string Auth) {
			string data = "test";
			//if (!Crypto.CheckMinuteCypher(Auth, 30)) throw new UserErrorException("Auth failed");
			DateTime? planningDate = null;
			bool planningDate_PLDT_IsTentativeDate = false;
			string backOrderDate = ViewModel.GetBackOrderDate(Warehouse_WHLO, ItemNumber_ITNO, 1, null, ref planningDate, ref planningDate_PLDT_IsTentativeDate);
			JsonpResult result = new JsonpResult(new ETAInfo() { PlanningDate=planningDate, IsTentativeDate=planningDate_PLDT_IsTentativeDate, PlanningDateText=backOrderDate});
			return result;
		}
		public class JsonpResult : JsonResult {
			object data = null;

			public JsonpResult() {
			}

			public JsonpResult(object data) {
				this.data = data;
			}

			public override void ExecuteResult(ControllerContext controllerContext) {
				if (controllerContext != null) {
					HttpResponseBase Response = controllerContext.HttpContext.Response;
					HttpRequestBase Request = controllerContext.HttpContext.Request;

					string callbackfunction = Request["callback"];
					if (string.IsNullOrEmpty(callbackfunction)) {
						throw new Exception("Callback function name must be provided in the request!");
					}
					Response.ContentType = "application/x-javascript";
					if (data != null) {
						JavaScriptSerializer serializer = new JavaScriptSerializer();
						Response.Write(string.Format("{0}({1});", callbackfunction, serializer.Serialize(data)));
					}
				}
			}
		}

javascript:

	var row = $(this);
				//string Warehouse_WHLO,string ItemNumber_ITNO, string Auth
				$.ajax({
					url: ""+ForecastFullURL+"/WholegoodsOrderPanel/GetETA",
					data: { Warehouse_WHLO:"100",ItemNumber_ITNO:"T500FM2EUR232", Auth:AuthToken },
					type: "GET",
					dataType: "jsonp",
					jsonp:"callback",
					success: function (data) {
						//alert(data);
						$(row).html('ok');
					}
				});

Comments


Leave a Comment