Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Made $.post jQuery-compatible and added tests for it
  • Loading branch information
Jakub Nesetril committed Nov 24, 2010
1 parent e7299c3 commit f178646
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/ajax.js
@@ -1,19 +1,25 @@
(function($){
function ajax(method, url, success, data){
function ajax(method, url, success, data, type){
data = data || null;
if (data instanceof Object) data = JSON.stringify(data);
var r = new XMLHttpRequest();
r.onreadystatechange = function(){
if(r.readyState==4 && (r.status==200 || r.status==0))
success(r.responseText);
};
if (success instanceof Function) {
r.onreadystatechange = function(){
if(r.readyState==4 && (r.status==200 || r.status==0))
success(r.responseText);
};
}
r.open(method,url,true);
if (type) r.setRequestHeader("Accept", type );
if (data instanceof Object) data = JSON.stringify(data), r.setRequestHeader('Content-Type','application/json');
r.setRequestHeader('X-Requested-With','XMLHttpRequest');
r.send(data);
}

$.get = function(url, success){ ajax('GET', url, success); };
$.post = function(url, success, data){ ajax('POST', url, success, data); };
$.post = function(url, data, success, type){
if (data instanceof Function) type = type || success, success = data, data = null;
ajax('POST', url, success, data, type);
};
$.getJSON = function(url, success){
$.get(url, function(json){ success(JSON.parse(json)) });
};
Expand Down
18 changes: 18 additions & 0 deletions test/ajax.html
Expand Up @@ -35,6 +35,24 @@ <h1>Zepto Ajax unit tests</h1>
});
});
},

testAjaxPostWithData: function(t) {
t.pause();
$.post('fixtures/ajax_load_simple.html', { sample: 'data' }, function(response) {
t.resume(function() {
this.assert(response);
});
});
},

testAjaxPostWithAcceptType: function(t) {
t.pause();
$.post('fixtures/ajax_load_simple.html', { sample: 'data' }, function(response) {
t.resume(function() {
this.assert(response);
});
}, 'text/plain');
},

testAjaxGetJSON: function(t){
t.pause();
Expand Down

0 comments on commit f178646

Please sign in to comment.