Ticket #982 (closed defect: invalid)
& not encoded in JSON request
| Reported by: | mib | Owned by: | |
|---|---|---|---|
| Type: | defect | Priority: | major |
| Milestone: | Mootools version 1.2 | Component: | Core |
| Keywords: | json, amp, encode | Cc: |
Description
IN JSON request & is not URL encoded and PHP can't parse this request.
In our code I create following patch to fix this problem:
Json.toUrlEncodedString = function(obj) {
switch($type(obj)){
case 'string':
return '"' + encodeURIComponent(obj.replace(/(["\\])/g, '\\$1')) + '"';
case 'array':
return '[' + obj.map(Json.toUrlEncodedString).join(',') + ']';
case 'object':
var string = [];
for (var property in obj) string.push(Json.toUrlEncodedString(property) + ':' + Json.toUrlEncodedString(obj[property]));
return '{' + string.join(',') + '}';
case 'number':
if (isFinite(obj)) break;
case false:
return 'null';
}
return String(obj);
}
Json.Remote = XHR.extend({
initialize: function(url, options){
this.url = url;
this.addEvent('onSuccess', this.onComplete);
this.parent(options);
//this.setHeader('X-Request', 'JSON');
},
send: function(obj){
return this.parent(this.url, 'json=' + Json.toUrlEncodedString(obj));
},
onComplete: function(){
this.fireEvent('onComplete', [Json.evaluate(this.response.text, this.options.secure)]);
}
});
Change History
Note: See
TracTickets for help on using
tickets.