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