If I am understanding you correctly, you are looking to correlate your requests and responses via a unique value.
You could use context on jquery.ajax. This means you would have to "give up" load but since its really a wrapper around ajax you aren't losing anything besides a little typing.
Here is an example using similar code that you provided.
$(function () {
// Ensure if our request context doesn't have an ID we at least get something back.
function getContextRequestID(settings, defaultIfNotSet)
{
if (typeof settings.context.requestID !== "undefined")
{
return settings.context.requestID;
}
return defaultIfNotSet;
}
function writeToJsResult(method, settings)
{
var DefaultRequestID = -1;
var requestID = getContextRequestID(settings, DefaultRequestID);
$(".js-result").append('<p>' + method + ': Ajax request for page --> ' + settings.url + ' with ID of: ' + settings.context.requestID + '</p>');
}
$(document).ajaxSend(function (event, jqxhr, settings) {
writeToJsResult("Starting", settings);
});
$(document).ajaxError(function (event, req, settings) {
writeToJsResult("Error", settings);
});
$(document).ajaxComplete(function (event, req, settings) {
writeToJsResult("Complete", settings);
});
/* triggering all Ajax requests */
$('.js-btn-json').on('click', function () {
loadBBC();
loadBBC();
});
function loadBBC()
{
loadSite("http://www.bbc.com");
}
function loadCNN()
{
loadSite("http://www.cnn.com");
}
function loadSite(url)
{
// Not sure how you want to correlate them. For now using random.
// Perhaps a global running counter
// Perhaps a GUID/UUID
// http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
var requestID = getRandomInt(0, 100);
$.ajax({
url: url,
context: {
requestID: requestID
}
});
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
});