Lecture Notes for CS 142
Winter 2014
John Ousterhout
xhr = new XMLHttpRequest(); xhr.onreadystatechange = xhrHandler; xhr.open("POST", url); xhr.send(postData); ... function xhrHandler() { if (this.readyState != 4) { return; } if (this.status != 200) { // Handle error ... return; } ... var text = this.responseText; }
{name: "Alice", gpa: 3.5, friends: ["Bill", "Carol", "David"]}
@students = Student.find(:all) render :json => @students;
[{"advisor_id":"2","birth":"1987-10-22","gpa":3.9,"grad":2009,"id":1,"name":"Anderson"}, {"advisor_id":"1","birth":"1990-04-16","gpa":3.1,"grad":2012,"id":2,"name":"Jones"}, {"advisor_id":"1","birth":"1989-08-12","gpa":3.6,"grad":2011,"id":3,"name":"Hernandez"}, {"advisor_id":"1","birth":"1990-02-04","gpa":3.2,"grad":2011,"id":4,"name":"Chen"}]
var students = JSON.parse(xhr.responseText);
<iframe name="xyz"></iframe> ... <form target="xyz"> ... </form>