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