Skip to content

Commit

Permalink
Merge pull request madrobby#92 from marioestrada/zepto
Browse files Browse the repository at this point in the history
---

Sending a string as a data parameter like var1=something&var2=2 was not being handled correctly and no POST variables were being received on the server.
  • Loading branch information
madrobby committed Feb 26, 2011
2 parents 41599f8 + 6595f67 commit 073d51f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
23 changes: 20 additions & 3 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
callback = options.success || empty,
errback = options.error || empty,
mime = mimeTypes[options.dataType],
content = options.contentType,
type = options.type || "GET",
content = options.contentType || (type === "POST" ? "application/x-www-form-urlencoded" : ""),
xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(){
Expand All @@ -47,9 +48,9 @@
}
};

xhr.open(options.type || 'GET', options.url || window.location, true);
xhr.open(type, options.url || window.location, true);
if (mime) xhr.setRequestHeader('Accept', mime);
if (data instanceof Object) data = JSON.stringify(data), content = content || 'application/json';
if (data instanceof Object) data = $.param(data);
if (content) xhr.setRequestHeader('Content-Type', content);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.send(data);
Expand Down Expand Up @@ -81,4 +82,20 @@
});
return this;
};

$.param = function(obj, v){
var s = [],
rec = '',
add = function(key, value){
if(v) s[s.length] = encodeURIComponent(v + "[" + key +"]") + '=' + encodeURIComponent(value);
else s[s.length] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
};
for(var i in obj){
if(obj[i] instanceof Array || obj[i] instanceof Object)
rec += (s.length + rec.length > 0 ? '&' : '') + $.param(obj[i], (v ? v + "[" + i + "]" : i));
else
add(obj instanceof Array ? '' : i, obj[i]);
};
return s.join("&").replace(/%20/g, "+") + rec;
};
})(Zepto);
2 changes: 1 addition & 1 deletion src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
$.fn.trigger = function(event){
return this.each(function(){
var e = document.createEvent('Events');
this.dispatchEvent(e, e.initEvent(event, true, false));
this.dispatchEvent(e, e.initEvent(event, true, true));
});
};
})(Zepto);
37 changes: 31 additions & 6 deletions test/ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ <h1>Zepto Ajax unit tests</h1>

testAjaxPostWithData: function(t) {
t.pause();
$.post('fixtures/ajax_load_simple.html', { sample: 'data' }, function(response) {
$.post('fixtures/ajax_post_json_echo.php', { sample: 'data', letters: ['a', 'b', 'c'] }, function(response) {
t.resume(function() {
this.assert(response);
this.assertEqual(response, '{"sample":"data","letters":["a","b","c"]}');
});
});
},
Expand Down Expand Up @@ -176,9 +176,10 @@ <h1>Zepto Ajax unit tests</h1>
t.assertEqual("world", result.hello);
},

testDataOptionIsConvertedToJSONString: function(t) {
$.ajax({ data: {hello: 'world'} });
t.assertEqual('{"hello":"world"}', MockXHR.last.data);
testDataOptionIsConvertedToSerializedForm: function(t) {
$.ajax({ data: {hello: 'world', array: [1,2,3], object: { prop1: 'val', prop2: 2 } } });
MockXHR.last.data = decodeURIComponent(MockXHR.last.data);
t.assertEqual('hello=world&array[]=1&array[]=2&array[]=3&object[prop1]=val&object[prop2]=2', MockXHR.last.data);
},

testErrorCallback: function(t) {
Expand Down Expand Up @@ -206,7 +207,7 @@ <h1>Zepto Ajax unit tests</h1>
t.assert(!successFired);
t.assertEqual(MockXHR.last, xhr);
t.assertEqual('parsererror', status);
t.assert(exception.toString().match(/JSON/));
t.assert(exception.toString().match(/SyntaxError/));
},

test201ResponseIsSuccess: function(t) {
Expand Down Expand Up @@ -245,5 +246,29 @@ <h1>Zepto Ajax unit tests</h1>
}
});
</script>

<script>
Evidence.TestCase.extend('ZeptoAjaxHelperMethodsTest',{

testParamMethod: function(t) {
result = $.param({ libs: ['jQuery', 'script.aculo.us', 'Prototype', 'Dojo'] });
result = decodeURIComponent(result);
t.assertEqual(result, "libs[]=jQuery&libs[]=script.aculo.us&libs[]=Prototype&libs[]=Dojo");

result = $.param({ jquery: 'Javascript', rails: 'Ruby', django: 'Python' });
result = decodeURIComponent(result);
t.assertEqual(result, "jquery=Javascript&rails=Ruby&django=Python");

result = $.param({
title: "Some Countries",
list: ['Ecuador', 'Austria', 'England'],
capitals: { ecuador: 'Quito', austria: 'Vienna', GB: { england: 'London', scotland: 'Edinburgh'} }
});
result = decodeURIComponent(result);
t.assertEqual(result, "title=Some+Countries&list[]=Ecuador&list[]=Austria&list[]=England&capitals[ecuador]=Quito&capitals[austria]=Vienna&capitals[GB][england]=London&capitals[GB][scotland]=Edinburgh");
}

});
</script>
</body>
</html>
1 change: 1 addition & 0 deletions test/fixtures/ajax_post_json_echo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo json_encode($_POST);
7 changes: 7 additions & 0 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,13 @@ <h1>Zepto DOM unit tests</h1>
click($('div#delegate_test a').get(0));
t.assert(fired);
t.refuteEqual('#foo', window.location.hash);

fired = false;
if (window.location.hash.length) window.location.hash = '';
$('div#delegate_test').html('<a href="#bar"></a>');
$('div#delegate_test a').trigger('click');
t.assert(fired);
t.refuteEqual('#bar', window.location.hash);
},

testUndelegate: function(t){
Expand Down

0 comments on commit 073d51f

Please sign in to comment.