prototype-fix.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Override prototype.js' Ajax
  2. // To allow PUT and PROPFIND method
  3. Ajax.Request.prototype.request = function(url) {
  4. this.url = url;
  5. this.method = this.options.method;
  6. var params = Object.clone(this.options.parameters);
  7. if (!['get', 'post', 'put', 'propfind'].include(this.method)) {
  8. // if (!['get', 'post'].include(this.method)) {
  9. // simulate other verbs over post
  10. params['_method'] = this.method;
  11. this.method = 'post';
  12. }
  13. this.parameters = params;
  14. if (params = Hash.toQueryString(params)) {
  15. // when GET, append parameters to URL
  16. if (this.method == 'get')
  17. this.url += (this.url.include('?') ? '&' : '?') + params;
  18. else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
  19. params += '&_=';
  20. }
  21. try {
  22. if (this.options.onCreate) this.options.onCreate(this.transport);
  23. Ajax.Responders.dispatch('onCreate', this, this.transport);
  24. this.transport.open(this.method.toUpperCase(), this.url,
  25. this.options.asynchronous);
  26. if (this.options.asynchronous)
  27. setTimeout(function() { this.respondToReadyState(1) }.bind(this), 10);
  28. this.transport.onreadystatechange = this.onStateChange.bind(this);
  29. this.setRequestHeaders();
  30. this.body = (['post', 'put'].include(this.method)) ? (this.options.postBody || params) : null;
  31. this.transport.send(this.body);
  32. /* Force Firefox to handle ready state 4 for synchronous requests */
  33. if (!this.options.asynchronous && this.transport.overrideMimeType)
  34. this.onStateChange();
  35. }
  36. catch (e) {
  37. this.dispatchException(e);
  38. }
  39. }
  40. // To avoid properties added to Object by ometa.
  41. Ajax.Request.prototype.setRequestHeaders = function(url) {
  42. var headers = {
  43. 'X-Requested-With': 'XMLHttpRequest',
  44. 'X-Prototype-Version': Prototype.Version,
  45. 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
  46. };
  47. if (this.method == 'post') {
  48. headers['Content-type'] = this.options.contentType +
  49. (this.options.encoding ? '; charset=' + this.options.encoding : '');
  50. /* Force "Connection: close" for older Mozilla browsers to work
  51. * around a bug where XMLHttpRequest sends an incorrect
  52. * Content-length header. See Mozilla Bugzilla #246651.
  53. */
  54. if (this.transport.overrideMimeType &&
  55. (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
  56. headers['Connection'] = 'close';
  57. }
  58. // user-defined headers
  59. if (typeof this.options.requestHeaders == 'object') {
  60. var extras = this.options.requestHeaders;
  61. if (typeof extras.push == 'function')
  62. for (var i = 0, length = extras.length; i < length; i += 2)
  63. headers[extras[i]] = extras[i+1];
  64. else
  65. $H(extras).each(function(pair) { headers[pair.key] = pair.value });
  66. }
  67. for (var name in headers) {
  68. if (typeof headers[name] == "string") {
  69. this.transport.setRequestHeader(name, headers[name]);
  70. }
  71. }
  72. }