Skip to content

Instantly share code, notes, and snippets.

@robbypelssers
Created April 15, 2014 19:25
Show Gist options
  • Select an option

  • Save robbypelssers/10762521 to your computer and use it in GitHub Desktop.

Select an option

Save robbypelssers/10762521 to your computer and use it in GitHub Desktop.
Ajax Demo: ajax.js
require.config({
baseUrl: 'scripts',
paths: {
jquery: 'jquery-2.1.0.min'
}
});
requirejs(["jquery"], function($) {
/**
0 -> UNSENT
1 -> OPENED
2 -> HEADERS_RECEIVED
3 -> LOADING
4 -> DONE
**/
function handleStateChange() {
var readyState = request.readyState;
console.log(readyState);
switch(readyState) {
case 4:
processResponse(request.responseText);
break;
default:
break;
}
}
function nonEmptyString(value) {
return value !== "";
}
function processResponse(responseText) {
console.log("Processing responseText");
var lines = responseText.split('\n');
lines.filter(nonEmptyString).forEach(function(line) {
console.log("Processing line: " + line);
/**
* TODO: dynamically build a table for each line from the CSV file with JQuery
* appended as child within <div id="result"/>
**/
});
}
console.log("About to make Ajax request");
var request = new XMLHttpRequest();
request.open("GET", "data/persons.csv", true);
request.onreadystatechange = handleStateChange;
request.send();
console.log("Ajax request has been sent");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment