Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class HttpRequestBaseExtensions
- {
- public static bool IsjQueryAjaxRequest(this HttpRequestBase request)
- {
- if (request == null)
- throw new ArgumentNullException("request");
- return request["__JQUERYASYNCPOST"] == "true";
- }
- }
- if (Request.IsjQueryAjaxRequest())
- //some code here
- $('form input[type=submit]').click(function(evt) {
- //intercept submit button and use AJAX instead
- evt.preventDefault();
- $.ajax(
- {
- type: "POST",
- url: "<%= Url.Action("Create") %>",
- dataType: "json",
- data: { "__JQUERYASYNCPOST": "true" },
- success: function(data) {alert(':)');},
- error: function(res, textStatus, errorThrown) {alert(':(');}
- }
- );
- });
- if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
- return Json(...);
- else
- return View();
- post: function(url, data, callback, type) {
- var postIdentifier = {};
- if (jQuery.isFunction(data)) {
- callback = data;
- data = {};
- }
- else {
- postIdentifier = { __JQUERYASYNCPOST: true };
- jQuery.extend(data, postIdentifier);
- }
- return jQuery.ajax({
- type: "POST",
- url: url,
- data: data,
- success: callback,
- dataType: type
- });
- }
Add Comment
Please, Sign In to add comment