I am new to learning AJAX. My question is: How would you connect to a website using Javascript instead of Java? In Java, you could use the JSoup library and the following code.
Response res = Jsoup.connect(EDLINE_URL + "/InterstitialLogin.page").timeout(0).userAgent("Chrome/12.0.742.122").execute();
Map<String, String> preLogCookies = res.cookies();
System.out.println("Pre Login Cookies: " + preLogCookies);
res = Jsoup.connect(EDLINE_URL + "/post/InterstitialLogin.page")
.data("TCNK", "authenticationEntryComponent",
"submitEvent", "1",
"guestLoginEvent", "",
"enterClicked", "false",
"bscf", "",
"bscv", "",
"targetEntid", "",
"ajaxSupported", "yes",
"screenName", username,
"kclq", password)
.cookies(preLogCookies)
.userAgent("Chrome/12.0.742.122")
.method(Method.POST)
.timeout(0)
.execute();
loginCookies = res.cookies();
loginCookies.putAll(preLogCookies);
System.out.println("Login Cookies: " + loginCookies);
System.out.println("Connected to: " + res.url());
if (res.url().toString().contains("Notification.page")) {
System.out.println("Username and Password Incorrect");
System.exit(1);
}
return res;
}
Obviously, you can't use Jsoup in javascript. So, how could you achieve the same connection? What I am trying to do is provided a "username" and a "password" enter that into a website and then access the data provided in the user-specific page. I think that you would use an XMLHttpRequest object but I am not sure how to 'feed' the information into the object/what to do with the object. For reference to how I think it could be done:
var clientId = "MyApp";
var clientSecret = "MySecret";
var authorizationBasic = $.base64.btoa(clientId + ':' + clientSecret);
var request = new XMLHttpRequest();
request.open('POST', oAuth.AuthorizationServer, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.setRequestHeader('Authorization', 'Basic ' + authorizationBasic);
request.setRequestHeader('Accept', 'application/json');
request.send("username=John&password=Smith&grant_type=password");
request.onreadystatechange = function () {
if (request.readyState == 4) {
alert(request.responseText);
}
};
This code is 'irrelevant' and sample from a website...but could prove valuable in understanding what I am trying to do...I don't understand what is happening or how I could modify it though. Any help would be wonderful!