[drupal-devel] [feature] Ajax HTTPPost javascript function
Issue status update for http://drupal.org/node/23746 Project: Drupal Version: cvs Component: base system Category: feature requests Priority: normal Assigned to: Anonymous Reported by: Thox Updated by: Thox Status: patch Attachment: http://drupal.org/files/issues/drupal.js.patch (1.25 KB) I needed to make HTTP posts of some information for an Ajax chat module I'm testing, so I've added an HTTPPost() function to drupal.js. It submits information like an HTML form would (using x-www-form-urlencoded). I'm not sure if assuming all HTTPosts will be encoded in this way is the best approach. I've never used any other encoding, but that's just me. Thox
Nice functionality. Question about coding conventions; I assume that Drupal coding conventions apply to our js code as well? If so, I believe one should avoid the use of conditional statements that depend on the "line after" rule and use {} in all cases. I find this block especially disturbing to read: + if (callback_function) + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState == 4) + callback_function(xmlhttp.responseText, xmlhttp, callback_parameter) + } To me these are just accidents waiting to happen (with the brackets). -Robert
Issue status update for http://drupal.org/node/23746 Project: Drupal Version: cvs Component: base system Category: feature requests Priority: normal Assigned to: Anonymous Reported by: Thox Updated by: Thox Status: patch Attachment: http://drupal.org/files/issues/drupal.js_0.patch (1.29 KB) I've improved the coding style for a new patch. Thox Previous comments: ------------------------------------------------------------------------ May 27, 2005 - 15:57 : Thox Attachment: http://drupal.org/files/issues/drupal.js.patch (1.25 KB) I needed to make HTTP posts of some information for an Ajax chat module I'm testing, so I've added an HTTPPost() function to drupal.js. It submits information like an HTML form would (using x-www-form-urlencoded). I'm not sure if assuming all HTTPosts will be encoded in this way is the best approach. I've never used any other encoding, but that's just me.
Issue status update for http://drupal.org/node/23746 Project: Drupal Version: cvs Component: base system Category: feature requests Priority: normal Assigned to: Anonymous Reported by: Thox Updated by: altano Status: patch I'm developing an AJAX chat client for fun and I've come across a lot of issues with XMLHttpRequest. First off, every access of the object should be in a try catch block, as exceptions can be thrown (especially with the send method) for no decipherable reason. You should handle these errors as well (a failed send will require recreation of the XMLHttpRequest object... for some reason trying to reuse them presents problems). I just have a function that creates the object and call it as needed. Here is some code for initializing a more cross-browser XMLHttpRequest object which can be thrown into a function: var xmlhttp; /*@cc_on @*/ /*@if (@_jscript_version >= 5) // JScript gives us Conditional compilation, we can cope with old IE versions. // and security blocked creation of the objects. try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @end @*/ if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); xmlhttp.overrideMimeType('text/xml'); } I honestly don't think AJAX is ready yet (for whatever), but I thought I would try to be somewhat informative. altano Previous comments: ------------------------------------------------------------------------ May 27, 2005 - 15:57 : Thox Attachment: http://drupal.org/files/issues/drupal.js.patch (1.25 KB) I needed to make HTTP posts of some information for an Ajax chat module I'm testing, so I've added an HTTPPost() function to drupal.js. It submits information like an HTML form would (using x-www-form-urlencoded). I'm not sure if assuming all HTTPosts will be encoded in this way is the best approach. I've never used any other encoding, but that's just me. ------------------------------------------------------------------------ May 28, 2005 - 13:19 : Thox Attachment: http://drupal.org/files/issues/drupal.js_0.patch (1.29 KB) I've improved the coding style for a new patch.
Issue status update for http://drupal.org/node/23746 Project: Drupal Version: cvs Component: base system Category: feature requests Priority: normal Assigned to: Anonymous Reported by: Thox Updated by: Thox Status: patch I've got a working Drupal Ajax chat demo (using POST and GET requests) here: http://brandedthoughts.co.uk/chat I've not (yet) had any unexpected errors thrown. Thox Previous comments: ------------------------------------------------------------------------ May 27, 2005 - 15:57 : Thox Attachment: http://drupal.org/files/issues/drupal.js.patch (1.25 KB) I needed to make HTTP posts of some information for an Ajax chat module I'm testing, so I've added an HTTPPost() function to drupal.js. It submits information like an HTML form would (using x-www-form-urlencoded). I'm not sure if assuming all HTTPosts will be encoded in this way is the best approach. I've never used any other encoding, but that's just me. ------------------------------------------------------------------------ May 28, 2005 - 13:19 : Thox Attachment: http://drupal.org/files/issues/drupal.js_0.patch (1.29 KB) I've improved the coding style for a new patch. ------------------------------------------------------------------------ May 28, 2005 - 13:35 : altano I'm developing an AJAX chat client for fun and I've come across a lot of issues with XMLHttpRequest. First off, every access of the object should be in a try catch block, as exceptions can be thrown (especially with the send method) for no decipherable reason. You should handle these errors as well (a failed send will require recreation of the XMLHttpRequest object... for some reason trying to reuse them presents problems). I just have a function that creates the object and call it as needed. Here is some code for initializing a more cross-browser XMLHttpRequest object which can be thrown into a function: var xmlhttp; /*@cc_on @*/ /*@if (@_jscript_version >= 5) // JScript gives us Conditional compilation, we can cope with old IE versions. // and security blocked creation of the objects. try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @end @*/ if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); xmlhttp.overrideMimeType('text/xml'); } I honestly don't think AJAX is ready yet (for whatever), but I thought I would try to be somewhat informative.
Issue status update for http://drupal.org/node/23746 Project: Drupal Version: cvs Component: base system Category: feature requests Priority: normal Assigned to: Anonymous Reported by: Thox Updated by: altano Status: patch Depending on how often you poll for data I can see it not being an issue. I had mine going with 50ms delays and left it overnight before I saw the errors start to pop up. Call me crazy =\ altano Previous comments: ------------------------------------------------------------------------ May 27, 2005 - 15:57 : Thox Attachment: http://drupal.org/files/issues/drupal.js.patch (1.25 KB) I needed to make HTTP posts of some information for an Ajax chat module I'm testing, so I've added an HTTPPost() function to drupal.js. It submits information like an HTML form would (using x-www-form-urlencoded). I'm not sure if assuming all HTTPosts will be encoded in this way is the best approach. I've never used any other encoding, but that's just me. ------------------------------------------------------------------------ May 28, 2005 - 13:19 : Thox Attachment: http://drupal.org/files/issues/drupal.js_0.patch (1.29 KB) I've improved the coding style for a new patch. ------------------------------------------------------------------------ May 28, 2005 - 13:35 : altano I'm developing an AJAX chat client for fun and I've come across a lot of issues with XMLHttpRequest. First off, every access of the object should be in a try catch block, as exceptions can be thrown (especially with the send method) for no decipherable reason. You should handle these errors as well (a failed send will require recreation of the XMLHttpRequest object... for some reason trying to reuse them presents problems). I just have a function that creates the object and call it as needed. Here is some code for initializing a more cross-browser XMLHttpRequest object which can be thrown into a function: var xmlhttp; /*@cc_on @*/ /*@if (@_jscript_version >= 5) // JScript gives us Conditional compilation, we can cope with old IE versions. // and security blocked creation of the objects. try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @end @*/ if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); xmlhttp.overrideMimeType('text/xml'); } I honestly don't think AJAX is ready yet (for whatever), but I thought I would try to be somewhat informative. ------------------------------------------------------------------------ May 28, 2005 - 14:11 : Thox I've got a working Drupal Ajax chat demo (using POST and GET requests) here: http://brandedthoughts.co.uk/chat I've not (yet) had any unexpected errors thrown.
Issue status update for http://drupal.org/node/23746 Post a follow up: http://drupal.org/project/comments/add/23746 Project: Drupal Version: cvs Component: base system Category: feature requests Priority: normal Assigned to: Anonymous Reported by: Thox Updated by: Thox Status: patch Attachment: http://drupal.org/files/issues/drupal.js_2.patch (1.31 KB) My latest ajax-powered spellcheck module requires HTTPPost too. I'm keen to see this function in core, as opposed to putting it in the module itself. I've updated it to allow sending of raw text to the recieving page, where before it always encoded the text/object as form encoding. Thox Previous comments: ------------------------------------------------------------------------ May 27, 2005 - 16:57 : Thox Attachment: http://drupal.org/files/issues/drupal.js.patch (1.25 KB) I needed to make HTTP posts of some information for an Ajax chat module I'm testing, so I've added an HTTPPost() function to drupal.js. It submits information like an HTML form would (using x-www-form-urlencoded). I'm not sure if assuming all HTTPosts will be encoded in this way is the best approach. I've never used any other encoding, but that's just me. ------------------------------------------------------------------------ May 28, 2005 - 14:19 : Thox Attachment: http://drupal.org/files/issues/drupal.js_0.patch (1.29 KB) I've improved the coding style for a new patch. ------------------------------------------------------------------------ May 28, 2005 - 14:35 : altano I'm developing an AJAX chat client for fun and I've come across a lot of issues with XMLHttpRequest. First off, every access of the object should be in a try catch block, as exceptions can be thrown (especially with the send method) for no decipherable reason. You should handle these errors as well (a failed send will require recreation of the XMLHttpRequest object... for some reason trying to reuse them presents problems). I just have a function that creates the object and call it as needed. Here is some code for initializing a more cross-browser XMLHttpRequest object which can be thrown into a function: var xmlhttp; /*@cc_on @*/ /*@if (@_jscript_version >= 5) // JScript gives us Conditional compilation, we can cope with old IE versions. // and security blocked creation of the objects. try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @end @*/ if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); xmlhttp.overrideMimeType('text/xml'); } I honestly don't think AJAX is ready yet (for whatever), but I thought I would try to be somewhat informative. ------------------------------------------------------------------------ May 28, 2005 - 15:11 : Thox I've got a working Drupal Ajax chat demo (using POST and GET requests) here: http://brandedthoughts.co.uk/chat I've not (yet) had any unexpected errors thrown. ------------------------------------------------------------------------ May 28, 2005 - 23:02 : altano Depending on how often you poll for data I can see it not being an issue. I had mine going with 50ms delays and left it overnight before I saw the errors start to pop up. Call me crazy =\
Issue status update for http://drupal.org/node/23746 Post a follow up: http://drupal.org/project/comments/add/23746 Project: Drupal Version: cvs Component: base system Category: feature requests Priority: normal Assigned to: Anonymous Reported by: Thox Updated by: Thox Status: patch Attachment: http://drupal.org/files/issues/drupal.js_3.patch (2.72 KB) New patch, improved coding style of the whole drupal.js file. Thox Previous comments: ------------------------------------------------------------------------ 1117209448 : Thox Attachment: http://drupal.org/files/issues/drupal.js.patch (1.25 KB) I needed to make HTTP posts of some information for an Ajax chat module I'm testing, so I've added an HTTPPost() function to drupal.js. It submits information like an HTML form would (using x-www-form-urlencoded). I'm not sure if assuming all HTTPosts will be encoded in this way is the best approach. I've never used any other encoding, but that's just me. ------------------------------------------------------------------------ 1117286376 : Thox Attachment: http://drupal.org/files/issues/drupal.js_0.patch (1.29 KB) I've improved the coding style for a new patch. ------------------------------------------------------------------------ 1117287358 : altano I'm developing an AJAX chat client for fun and I've come across a lot of issues with XMLHttpRequest. First off, every access of the object should be in a try catch block, as exceptions can be thrown (especially with the send method) for no decipherable reason. You should handle these errors as well (a failed send will require recreation of the XMLHttpRequest object... for some reason trying to reuse them presents problems). I just have a function that creates the object and call it as needed. Here is some code for initializing a more cross-browser XMLHttpRequest object which can be thrown into a function: var xmlhttp; /*@cc_on @*/ /*@if (@_jscript_version >= 5) // JScript gives us Conditional compilation, we can cope with old IE versions. // and security blocked creation of the objects. try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @end @*/ if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); xmlhttp.overrideMimeType('text/xml'); } I honestly don't think AJAX is ready yet (for whatever), but I thought I would try to be somewhat informative. ------------------------------------------------------------------------ 1117289473 : Thox I've got a working Drupal Ajax chat demo (using POST and GET requests) here: http://brandedthoughts.co.uk/chat I've not (yet) had any unexpected errors thrown. ------------------------------------------------------------------------ 1117317736 : altano Depending on how often you poll for data I can see it not being an issue. I had mine going with 50ms delays and left it overnight before I saw the errors start to pop up. Call me crazy =\ ------------------------------------------------------------------------ 1121421984 : Thox Attachment: http://drupal.org/files/issues/drupal.js_2.patch (1.31 KB) My latest ajax-powered spellcheck module requires HTTPPost too. I'm keen to see this function in core, as opposed to putting it in the module itself. I've updated it to allow sending of raw text to the recieving page, where before it always encoded the text/object as form encoding.
participants (3)
-
altano -
Robert Douglass -
Thox