|
|
1
22
这是检索的正确语法 JSON with Prototype
|
|
|
2
3
Response:responseJSON有一个属性,只有当后端返回Content-Type:application/JSON时,它才会填充JSON对象,也就是说,如果你在后端代码中做了这样的事情:
在这种情况下,Response.responseJSON!=undefined,您可以在onSuccess(t)处理程序中在接收端检查它:
|
|
|
3
2
这来自Prototype官方:
|
|
|
4
1
//fetches comments from the server
CommentWidget.prototype.getComments = function() {
var commentURL = this.getCommentsURL + this.obj.type + '/' + this.obj.id;
this.asyncRequest('GET', commentURL, null);
}
//initiates an XHR request
CommentWidget.prototype.asyncRequest = function(method, uri, form) {
var o = createXhrObject()
if(!o) { return null; }
o.open(method, uri, true);
o.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
var self = this;
o.onreadystatechange = function () {self.callback(o)};
if (form) {
o.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
o.send(makePostData(form));
} else {
o.send('');
}
}
//after a comment is posted, this rewrites the comments on the page
CommentWidget.prototype.callback = function(o) {
if (o.readyState != 4) { return }
//turns the JSON string into a JavaScript object.
var response_obj = eval('(' + o.responseText + ')');
this.comments = response_obj.comments;
this.refresh()
}
|