Created
May 31, 2016 10:31
-
-
Save jannesaranpaa/13c0b9f6fcdd82d9dbef9729fc6dd855 to your computer and use it in GitHub Desktop.
jQuery HTTP
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
| class json | |
| { | |
| static encode(object) | |
| { | |
| return JSON.stringify(object); | |
| } | |
| static decode(string) | |
| { | |
| console.log(string); | |
| return JSON.parse(string); | |
| } | |
| } | |
| class Request | |
| { | |
| static send(method, url, handler) | |
| { | |
| $.ajax({ | |
| type: "GET", | |
| //headers: {"X-My-Custom-Header": "some value"}, | |
| url: url, | |
| contentType: "text/json", | |
| xhrFields: | |
| { | |
| withCredentials: false, | |
| } | |
| }) | |
| .done(function(data) { | |
| console.log("success"); | |
| //console.log(data); | |
| handler(data); | |
| }) | |
| .fail(function(error) { | |
| console.log(error); | |
| handler(error); | |
| }) | |
| .always(function() { | |
| //console.log("complete"); | |
| }); | |
| } | |
| } |
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
| function parse_json(data) | |
| { | |
| console.log(data); | |
| var i = 0; | |
| for (var item in data) | |
| { | |
| var student = data[item]; | |
| for (var subject in student) | |
| { | |
| var grade = student[subject]; | |
| console.log(item, subject, grade); | |
| i++; | |
| $("table#grades").append("<tr> <td>" + i + "</td><td>" + item + "</td><td>" + subject + "</td><td>" + grade + "</td> </tr>"); | |
| } | |
| } | |
| } | |
| function main() | |
| { | |
| Request.send("GET", "http://salokoski.fi/Janne/tests/webservice/index.php", parse_json); | |
| } | |
| $(document).ready(function() | |
| { | |
| main(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment