Created
December 27, 2012 16:39
-
-
Save dpoindexter/4389669 to your computer and use it in GitHub Desktop.
jQuery configuration to simplify use of AjaxPro with $.ajax API. Some of the weirdness of AjaxPro responses may have been fixed in the most recent version.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; (function($) { | |
/* For AJAX calls, defines a custom type "ajaxproresult" and how to convert from text type to | |
ajaxproresult. This allows us to convert both AjaxPro success (valid JSON) and exception | |
(invalid JSON, some kind of script result) to JSON */ | |
$.ajaxSetup({ | |
converters: { | |
//Converts AjaxPro responses to JSON, and handles the weird non-JSON string that AjaxPro returns on exception | |
'text ajaxproresult': function(textValue) { | |
//Every AjaxPro response ends with ;/* | |
textValue = textValue.replace(';/*', ''); | |
//If there was a server-side exception, AjaxPro returns a JSON-ish string after r.error = | |
var err = textValue.indexOf('r.error'); | |
if (err !== -1) { | |
//Grab just the JSON-ish part of the error, add an exception: true property and parse into a JS object | |
var jsonPart = textValue.substring(err + 10); | |
return $.extend({}, { exception: true }, JSON.parse(jsonPart)); | |
} else { | |
return JSON.parse(textValue); | |
} | |
} | |
} | |
}); | |
/* Wrapper for jQuery.ajax that configures headers and responses properly for AjaxPro. | |
Otherwise, API is identical to http://api.jquery.com/jQuery.ajax */ | |
$.ajaxPro = function(options) { | |
if (!options.method) throw new Error('You must specify an AjaxPro method to call'); | |
//Settings that apply specifically to AjaxPro | |
var overrides = { | |
//AjaxPro seems to have trouble handling its param-matching routine with GET requests | |
type: 'POST', | |
url: options.url, | |
headers: { | |
//This is the server-side method marked with the [AjaxMethod] attribute | |
'X-AjaxPro-Method': options.method, | |
'X-uShip-LangCulture': (uship.prefs) ? uship.prefs.i18n.langculture : 'en-US' | |
}, | |
//AjaxPro expects the server-side method params and the POST body to be JSON instead of | |
//form-encoded | |
data: JSON.stringify(options.data), | |
//This is a custom datatype that allows $.ajaxSetup converters to run and handle | |
//AjaxPro's returned weirdness | |
dataType: 'ajaxproresult' | |
}; | |
var ajaxOptions = $.extend({}, options, overrides); | |
//By wrapping the call to $.ajax in another Deferred, we can check the exception property we | |
//get back from AjaxPro, and make the deferred return "fail" like it would with a 4XX HTTP | |
//status code | |
return $.Deferred(function(dfd){ | |
$.ajax(ajaxOptions).done(function(data) { | |
data.exception ? dfd.reject(data) : dfd.resolve(data); | |
}); | |
}); | |
} | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! you really save me with this. A little update: "method" prop is now an alias for "type" prop in $.ajax so the request is sent with a HTTP method other than POST. I fixed manually in my code but if you want to update this choose the prop name as you please.
PD: english is not native language.