Created
April 15, 2014 19:25
-
-
Save robbypelssers/10762521 to your computer and use it in GitHub Desktop.
Ajax Demo: ajax.js
This file contains hidden or 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
| 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