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);

        }
示例#3
0
        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);

        }
        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 hidden field to the body
            if (body.Length > 0 && !body.EndsWith('&')) {
                body += "&";
            }
            body += "X-Requested-With=XMLHttpRequest";

            // Determine where to place the body
            string requestBody = "";
            if (verb.ToUpperCase() == "GET" || verb.ToUpperCase() == "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;
            request.HttpVerb = verb;
            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();
            }
        }
        internal static void OnComplete(WebRequest request, AjaxOptions ajaxOptions, AjaxContext ajaxContext) {
            // Update the AjaxContext
            ajaxContext.Response = request.Executor;

            // Run onComplete and check for cancellation
            // 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"
            if (ajaxOptions.OnComplete != null && (object)ajaxOptions.OnComplete(ajaxContext) == (object)false) {
                return;
            }

            // If the status code was successful...
            int statusCode = ajaxContext.Response.StatusCode;
            if ((statusCode >= 200 && statusCode < 300) || statusCode == 304 || statusCode == 1223) {
                // If the status code is one of 204 (No Content), 304 (Not Modified), or 1223 (IE-specific code caused by 204), don't do the injection
                if (statusCode != 204 && statusCode != 304 && statusCode != 1223) {
                    string contentType = ajaxContext.Response.GetResponseHeader("Content-Type");
                    if ((contentType != null) && (contentType.IndexOf("application/x-javascript") != -1)) {
                        Script.Eval(ajaxContext.Data);
                    }
                    else {
                        UpdateDomElement(ajaxContext.UpdateTarget, ajaxContext.InsertionMode, ajaxContext.Data);
                    }
                }

                if (ajaxOptions.OnSuccess != null) {
                    ajaxOptions.OnSuccess(ajaxContext);
                }
            }
            else {
                if (ajaxOptions.OnFailure != null) {
                    ajaxOptions.OnFailure(ajaxContext);
                }
            }

            // Hide the loading panel, if there is one
            if (ajaxContext.LoadingElement != null) {
                Type.InvokeMethod(typeof(Sys.UI.DomElement), "setVisible", ajaxContext.LoadingElement, false);
            }
        }