Another possibility is instead of doing dataString
do dataObject
then pass that object to the callback. Like so:
function getGrades(grading_company) {
if (grading_company == 'Not Specified') {
// Remove grades box & show condition box
showConditionBox();
} else {
// Set file to get results from..
var loadUrl = "ajax_files/get_grades.php";
// Set data object
var dataObject = {
'gc_id' : grading_company
/*to do multiples..
'item1' : value1,
'item2' : value2,
'etc' : etc */
}
// Set the callback function to run on success
var callback = showGradesBox;
// Run the AJAX request
runAjax(loadUrl, dataObject, callback);
}
}
function runAjax(loadUrl, dataObject, callback) {
jQuery.ajax({
type: 'GET',
url: loadUrl,
data: $.param(dataObject),
dataType: 'html',
error: ajaxError,
success: function(response) {
callback(response, dataObject);
}
});
}
Note the addition of $.param()
.
Then in the callback function, you should know what data you're after. If function setGrades(resp, data) { ... }
was the callback, then you can access the values in setGrades
function setGrades(resp, data) {
alert( data.gc_id);
}
EDIT
After testing, I realize that $(dataObject).serialize()
will not work. So I've updated to use $.param()
. Please see this SO post for more info.