From c1cff6b4f451552ede0a351e43699c53dfe157df Mon Sep 17 00:00:00 2001 From: schneefux Date: Sat, 15 Mar 2014 16:44:01 +0100 Subject: benutze jQuery für Ajax-Requests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/index.js | 55 ++++++++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/js/index.js b/js/index.js index 66b6d4e..53e905b 100644 --- a/js/index.js +++ b/js/index.js @@ -17,22 +17,18 @@ */ function get_overview(){ - var req = new XMLHttpRequest(); + var request = $.ajax({ + type: "GET", + url: "http://***REMOVED***/moodle/course/view.php?id=2" + }); - req.onreadystatechange = function(){ - if(req.readyState == 4){ - if(req.status == 200 || window.location.href.indexOf("http") != -1){ - console.log('Übersichts-Request erfolgreich'); - return 1; - }else{ - console.log('Etwas ist schiefgelaufen :('); - return; - } - } - } + request.done(function(msg){ + console.log('Übersichts-Request erfolgreich'); + }); - req.open("GET", "http://***REMOVED***/moodle/course/view.php?id=2", true); - req.send(); + request.fail(function(jqXHR, msg){ + alert("Etwas ist schiefgelaufen: " + textStatus); + }); } function login(){ @@ -42,30 +38,23 @@ function login(){ theme: "b", }); - var req = new XMLHttpRequest(); - var username = encodeURIComponent($("#username").value); var password = encodeURIComponent($("#password").value); var params = "username=" + username + "&password=" + password; - req.onreadystatechange = function(){ - if(req.readyState == 4){ - if(req.status == 0){ - console.log("Es fehlt die Berechtigung, um auf den Server zuzugreifen."); - return; - }else if(req.status != 200 && window.location.href.indexOf("http") != -1){ - console.log("Etwas ist schiefgelaufen :("); - return; - }else{ - console.log('Login-Request erfolgreich'); - get_overview(); - } - } - } + var request = $.ajax({ + type: "POST", + url: "https://***REMOVED***/moodle/login/index.php", + data: {username: username, password: password} + }); + request.done(function(msg){ + console.log('Login-Request erfolgreich'); + get_overview(); + }); - req.open("POST", "https://***REMOVED***/moodle/login/index.php", true); - req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); - req.send(params); + request.fail(function(jqXHR, textStatus){ + alert("Etwas ist schiefgelaufen: " + textStatus); + }); } var app = { -- cgit v1.3.1