Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #25619

Re: XHR giving 405

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: XHR giving 405
Date 2014-08-03 19:16 +0200
Organization PointedEars Software (PES)
Message-ID <17377960.EH8SsSYXrz@PointedEars.de> (permalink)
References (1 earlier) <4e2chb4g81mw.3qus2sg5140u$.dlg@40tude.net> <1789411.yMKPfb8zHj@PointedEars.de> <ja2dncw4V82KWEDOnZ2dnUVZ_tadnZ2d@westnet.com.au> <11731815.6KERGQWCj8@PointedEars.de> <1PqdndNVipAMY0DOnZ2dnUVZ_oydnZ2d@westnet.com.au>

Show all headers | View raw


Andrew Poulos wrote:

> On 3/08/2014 5:43 PM, Thomas 'PointedEars' Lahn wrote:
>> Andrew Poulos wrote:
>>> I changed their code to
>>>
>>> fullUrl += "/";
>>> if (pairs.length > 0) {
>>>     fullUrl += "?" + pairs.join("&");
>>> }
>>> […]
>>
>> […]
>> Do they encodeURIComponent() the elements of “pairs” before join()ing it?
>
> I think so. I found this
> 
> pairs.push(prop + "=" + encodeURIComponent(cfg.params[prop]));

Apparently they have neglected to encode the parameter name as well.

This looks like it could be from

  var fullUrl = "";
  var pairs = [];

  // …

  for (var prop in cfg.params) {
    if (cfg.params.hasOwnProperty(prop)) {
      pairs.push(prop + "=" + encodeURIComponent(cfg.params[prop]));
    }
  }

  fullUrl += "/";
  if (pairs.length > 0) {
    fullUrl += "?" + pairs.join("&");
  }

If so, the following would be safer and could be more efficient, provided 
the used ES-5+-specified methods are either built-in or emulated:

  var o = cfg.params;

  var pairs = Object.keys(o).map(
    function (key) {
      return [key, this[key]].map(encodeURIComponent).join("=");
    },
    o
  ).join("&");

  // …

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys>
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

XHR giving 405 Andrew Poulos <ap_prog@hotmail.com> - 2014-07-31 15:34 +1000
  Re: XHR giving 405 Andrew Poulos <ap_prog@hotmail.com> - 2014-07-31 16:45 +1000
    Re: XHR giving 405 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-07-31 09:58 +0200
    Re: XHR giving 405 Scott Sauyet <scott.sauyet@gmail.com> - 2014-08-01 11:20 -0700
  Re: XHR giving 405 JJ <jj4public@vfemail.net> - 2014-07-31 14:20 +0700
    Re: XHR giving 405 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-08-03 00:08 +0200
      Re: XHR giving 405 Andrew Poulos <ap_prog@hotmail.com> - 2014-08-03 15:10 +1000
        Re: XHR giving 405 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-08-03 09:43 +0200
          Re: XHR giving 405 Andrew Poulos <ap_prog@hotmail.com> - 2014-08-03 19:16 +1000
            Re: XHR giving 405 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-08-03 19:16 +0200
              Re: XHR giving 405 Andrew Poulos <ap_prog@hotmail.com> - 2014-08-04 07:29 +1000
      Re: XHR giving 405 John Harris <niam@jghnorth.org.uk.invalid> - 2014-08-03 16:46 +0100
        Re: XHR giving 405 Tim Streater <timstreater@greenbee.net> - 2014-08-03 16:51 +0100
        Re: XHR giving 405 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-08-03 18:39 +0200
          Re: XHR giving 405 John Harris <niam@jghnorth.org.uk.invalid> - 2014-08-04 15:08 +0100
            Re: XHR giving 405 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-08-04 19:12 +0200
              Re: XHR giving 405 John Harris <niam@jghnorth.org.uk.invalid> - 2014-08-06 14:25 +0100
                Re: XHR giving 405 Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-08-06 20:20 +0100
                Re: XHR giving 405 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-08-07 21:13 +0200

csiph-web