public static void HandleSubmit(FormElement form, DomEvent evt, AjaxOptions ajaxOptions) { evt.PreventDefault(); // run validation ArrayList validationCallbacks = (ArrayList)Type.GetField(form, "validationCallbacks"); if (validationCallbacks != null) { for (int i = 0; i < validationCallbacks.Length; i++) { ValidationCallback callback = (ValidationCallback)validationCallbacks[i]; if (!callback()) { return; // bail out since validation failed } } } string body = MvcHelpers.SerializeForm(form); MvcHelpers.AsyncRequest(form.Action, form.Method ?? "post", body, form, ajaxOptions); }
public static void HandleClick(AnchorElement anchor, DomEvent evt, AjaxOptions ajaxOptions) { evt.PreventDefault(); MvcHelpers.AsyncRequest(anchor.Href, "post", "", anchor, ajaxOptions); }
public static void HandleSubmit(FormElement form, DomEvent evt, AjaxOptions ajaxOptions) { evt.PreventDefault(); string body = MvcHelpers.SerializeForm(form); MvcHelpers.AsyncRequest(form.Action, form.Method ?? "post", body, form, ajaxOptions); }
internal static void AsyncRequest(string url, string verb, string body, DOMElement triggerElement, AjaxOptions ajaxOptions) { // Run the confirm popup, if specified if (ajaxOptions.Confirm != null) { if (!Script.Confirm(ajaxOptions.Confirm)) { return; } } // Override the url if specified in AjaxOptions if (ajaxOptions.Url != null) { url = ajaxOptions.Url; } // Override the verb if specified in AjaxOptions if (ajaxOptions.HttpMethod != null) { verb = ajaxOptions.HttpMethod; } // Add the special hidden fields to the body if (body.Length > 0 && !body.EndsWith('&')) { body += "&"; } body += "X-Requested-With=XMLHttpRequest"; string upperCaseVerb = verb.ToUpperCase(); bool isGetOrPost = (upperCaseVerb == "GET" || upperCaseVerb == "POST"); if (!isGetOrPost) { body += "&"; body += "X-HTTP-Method-Override=" + upperCaseVerb; } // Determine where to place the body string requestBody = ""; if (upperCaseVerb == "GET" || upperCaseVerb == "DELETE") { if (url.IndexOf('?') > -1) { // Case 1: http://foo.bar/baz?abc=123 if (!url.EndsWith('&')) { url += "&"; } url += body; } else { // Case 2: http://foo.bar/baz url += "?"; url += body; } } else { requestBody = body; } // Create the request WebRequest request = new WebRequest(); request.Url = url; // Some browsers only support XMLHttpRequest with GET and POST. Just to be // safe we restrict out requests to use only those two methods and use a // header as well as a form post field to override the verb. On the server side // the header and form post field are supported using the AcceptVerbs attribute. if (isGetOrPost) { request.HttpVerb = verb; } else { request.HttpVerb = "POST"; request.Headers["X-HTTP-Method-Override"] = upperCaseVerb; } request.Body = requestBody; if (verb.ToUpperCase() == "PUT") { request.Headers["Content-Type"] = "application/x-www-form-urlencoded;"; } request.Headers["X-Requested-With"] = "XMLHttpRequest"; DOMElement updateElement = null; if (ajaxOptions.UpdateTargetId != null) { updateElement = Document.GetElementById(ajaxOptions.UpdateTargetId); } DOMElement loadingElement = null; if (ajaxOptions.LoadingElementId != null) { loadingElement = Document.GetElementById(ajaxOptions.LoadingElementId); } // Create the AjaxContext for the request AjaxContext ajaxContext = new AjaxContext(request, updateElement, loadingElement, ajaxOptions.InsertionMode); // Run onBegin and check for cancellation bool continueRequest = true; if (ajaxOptions.OnBegin != null) { // Have to convert to objects to force the "!== false" to be emitted. // We want no return value to be treated as returning true, so we only want to cancel the request if the result is exactly "false" continueRequest = (object)ajaxOptions.OnBegin(ajaxContext) != (object)false; } // Display the loading element, if specified if (loadingElement != null) { Type.InvokeMethod(typeof(Sys.UI.DomElement), "setVisible", ajaxContext.LoadingElement, true); } if (continueRequest) { // Setup the callback request.Completed += delegate(WebRequestExecutor executor) { MvcHelpers.OnComplete(request, ajaxOptions, ajaxContext); }; request.Invoke(); } }
public static void HandleClick(FormElement form, DomEvent evt) { string additionalInput = MvcHelpers.SerializeSubmitButton(evt.Target, evt.OffsetX, evt.OffsetY); Type.SetField(form, "_additionalInput", additionalInput); }