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


Groups > comp.lang.javascript > #15725 > unrolled thread

XHR: Passing Parameters to a ready-state-change Function

Started byGene Wirchenko <genew@ocis.net>
First post2012-09-05 13:53 -0700
Last post2012-09-11 22:42 +0100
Articles 20 on this page of 38 — 11 participants

Back to article view | Back to comp.lang.javascript


Contents

  XHR: Passing Parameters to a ready-state-change Function Gene Wirchenko <genew@ocis.net> - 2012-09-05 13:53 -0700
    Re: XHR: Passing Parameters to a ready-state-change Function Jim T. <x@y.z> - 2012-09-05 17:46 -0400
      Re: XHR: Passing Parameters to a ready-state-change Function Gene Wirchenko <genew@ocis.net> - 2012-09-05 17:30 -0700
        Re: XHR: Passing Parameters to a ready-state-change Function Jim T. <x@y.z> - 2012-09-05 20:55 -0400
    Re: XHR: Passing Parameters to a ready-state-change Function Jim T. <x@y.z> - 2012-09-05 21:43 -0400
      Re: XHR: Passing Parameters to a ready-state-change Function Jim T. <x@y.z> - 2012-09-05 21:49 -0400
    Re: XHR: Passing Parameters to a ready-state-change Function Stefan Weiss <krewecherl@gmail.com> - 2012-09-06 18:56 +0200
      Re: XHR: Passing Parameters to a ready-state-change Function Gene Wirchenko <genew@ocis.net> - 2012-09-06 10:55 -0700
      Re: XHR: Passing Parameters to a ready-state-change Function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-06 21:38 +0200
        Re: XHR: Passing Parameters to a ready-state-change Function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-06 21:49 +0200
      Re: XHR: Passing Parameters to a ready-state-change Function Gene Wirchenko <genew@ocis.net> - 2012-09-06 15:49 -0700
        Re: XHR: Passing Parameters to a ready-state-change Function Tim Streater <timstreater@greenbee.net> - 2012-09-07 09:15 +0100
          Re: XHR: Passing Parameters to a ready-state-change Function Gene Wirchenko <genew@ocis.net> - 2012-09-07 09:58 -0700
            Re: XHR: Passing Parameters to a ready-state-change Function Tim Streater <timstreater@greenbee.net> - 2012-09-07 19:14 +0100
              Re: XHR: Passing Parameters to a ready-state-change Function cezary.tomczyk@gmail.com - 2012-09-07 13:51 -0700
                Re: XHR: Passing Parameters to a ready-state-change Function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-08 00:52 +0200
                  Re: XHR: Passing Parameters to a ready-state-change Function cezary.tomczyk@gmail.com - 2012-09-07 16:32 -0700
                    Re: XHR: Passing Parameters to a ready-state-change Function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-08 09:48 +0200
                      Re: XHR: Passing Parameters to a ready-state-change Function Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-09-08 02:09 -0700
                    Re: XHR: Passing Parameters to a ready-state-change Function Tim Streater <timstreater@greenbee.net> - 2012-09-08 11:09 +0100
                      Re: XHR: Passing Parameters to a ready-state-change Function Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-09-08 03:39 -0700
                  Re: XHR: Passing Parameters to a ready-state-change Function John G Harris <john@nospam.demon.co.uk> - 2012-09-08 15:00 +0100
              Re: XHR: Passing Parameters to a ready-state-change Function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-07 23:44 +0200
                Re: XHR: Passing Parameters to a ready-state-change Function Tim Streater <timstreater@greenbee.net> - 2012-09-07 23:08 +0100
                  Re: XHR: Passing Parameters to a ready-state-change Function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-08 00:46 +0200
                    Re: XHR: Passing Parameters to a ready-state-change Function Tim Streater <timstreater@greenbee.net> - 2012-09-08 11:11 +0100
                      Re: XHR: Passing Parameters to a ready-state-change Function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-08 14:21 +0200
                        Re: XHR: Passing Parameters to a ready-state-change Function Tim Streater <timstreater@greenbee.net> - 2012-09-08 17:06 +0100
                          Re: XHR: Passing Parameters to a ready-state-change Function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-08 20:41 +0200
                            Re: XHR: Passing Parameters to a ready-state-change Function Dr J R Stockton <reply1236@merlyn.demon.co.uk.invalid> - 2012-09-09 20:04 +0100
              Re: XHR: Passing Parameters to a ready-state-change Function Gene Wirchenko <genew@ocis.net> - 2012-09-09 18:40 -0700
                Re: XHR: Passing Parameters to a ready-state-change Function "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-09-10 07:21 +0000
                Re: XHR: Passing Parameters to a ready-state-change Function Tim Streater <timstreater@greenbee.net> - 2012-09-10 09:03 +0100
                  Re: XHR: Passing Parameters to a ready-state-change Function "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-09-10 13:59 +0000
                    Re: XHR: Passing Parameters to a ready-state-change Function Tim Streater <timstreater@greenbee.net> - 2012-09-10 15:35 +0100
                      Re: XHR: Passing Parameters to a ready-state-change Function "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-09-10 22:42 +0000
                        Re: XHR: Passing Parameters to a ready-state-change Function Scott Sauyet <scott.sauyet@gmail.com> - 2012-09-11 10:09 -0700
                          Re: XHR: Passing Parameters to a ready-state-change Function Tim Streater <timstreater@greenbee.net> - 2012-09-11 22:42 +0100

Page 1 of 2  [1] 2  Next page →


#15725 — XHR: Passing Parameters to a ready-state-change Function

FromGene Wirchenko <genew@ocis.net>
Date2012-09-05 13:53 -0700
SubjectXHR: Passing Parameters to a ready-state-change Function
Message-ID<09ef48tfsgucbl7m49ggao3be5r0kb7v13@4ax.com>
Dear JavaScripters:

     Is there any way to pass parameters to the ready-state-change
function that checks the progress of a request?  I am currently using
a global.  I would prefer not to.

     What I want to pass is the name of a data-handling function to be
executed if there is data returned.  To me, it would be neater to do:

oRequest.onreadystatechange=StateChange somehow passing DataHandle1 or
2 to StateChange() as parm ProcessData
and
function StateChange()
  {
  <state change handling>
  ProcessData();     // Handle the data received.
  }

rather than

oRequest.onreadystatechange=DataHandle1 (or 2);
and
function StateChange()
  {
  <state change handling>
  }
function DataHandle1()
  {
  StateChange();
  <process data for scenario 1>
  }
function DataHandle2()
  {
  StateChange();
  <process data for scenario 2>
  }

Sincerely,

Gene Wirchenko

[toc] | [next] | [standalone]


#15726

FromJim T. <x@y.z>
Date2012-09-05 17:46 -0400
Message-ID<hkhf48hfgl5qj69tsn82579raqj9cbgo6b@4ax.com>
In reply to#15725
On Wed, 05 Sep 2012 13:53:26 -0700, Gene Wirchenko <genew@ocis.net>
wrote:

>Dear JavaScripters:
>
>     Is there any way to pass parameters to the ready-state-change
>function that checks the progress of a request?  I am currently using
>a global.  I would prefer not to.
>
>     What I want to pass is the name of a data-handling function to be
>executed if there is data returned.  To me, it would be neater to do:
>
>oRequest.onreadystatechange=StateChange somehow passing DataHandle1 or
>2 to StateChange() as parm ProcessData
>and
>function StateChange()
>  {
>  <state change handling>
>  ProcessData();     // Handle the data received.
>  }
>
>rather than
>
>oRequest.onreadystatechange=DataHandle1 (or 2);
>and
>function StateChange()
>  {
>  <state change handling>
>  }
>function DataHandle1()
>  {
>  StateChange();
>  <process data for scenario 1>
>  }
>function DataHandle2()
>  {
>  StateChange();
>  <process data for scenario 2>
>  }
>
>Sincerely,
>
>Gene Wirchenko

Something like this maybe?

oRequest.onreadystatechange=
    function()
    {
        var state = 0;
        
        return function()
                   {
                      state++;

                      if ( state == 1)
                          <process data for scenario 1>
                     else
                          <process data for scenario 2>
                  };
    } ();



[toc] | [prev] | [next] | [standalone]


#15728

FromGene Wirchenko <genew@ocis.net>
Date2012-09-05 17:30 -0700
Message-ID<v8rf48hqo7dppp25f3f9ocd8euu9bmseb6@4ax.com>
In reply to#15726
On Wed, 05 Sep 2012 17:46:49 -0400, Jim T. <x@y.z> wrote:

>On Wed, 05 Sep 2012 13:53:26 -0700, Gene Wirchenko <genew@ocis.net>
>wrote:
>
>>Dear JavaScripters:
>>
>>     Is there any way to pass parameters to the ready-state-change
>>function that checks the progress of a request?  I am currently using
>>a global.  I would prefer not to.

[snip]

>Something like this maybe?
>
>oRequest.onreadystatechange=
>    function()
>    {
>        var state = 0;
>        
>        return function()
>                   {
>                      state++;
>
>                      if ( state == 1)
>                          <process data for scenario 1>
>                     else
>                          <process data for scenario 2>
>                  };
>    } ();

     Uh, no.  The "1" and "2" in the names were not meant to imply any
order.

     I am working on the general design for Web pages which would
allow CRUD.  Depending on the request to be performed, the result back
from the server will be handled differently.  Request types could be
in any order.

     I also prefer to have each scenario broken out into its own
function.  IMHO, debugging Web code is mucky enough without having
longer-than-necessary functions.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#15729

FromJim T. <x@y.z>
Date2012-09-05 20:55 -0400
Message-ID<3vsf48lt9uhh3qfddvcgbllllqj9hvquva@4ax.com>
In reply to#15728
On Wed, 05 Sep 2012 17:30:39 -0700, Gene Wirchenko <genew@ocis.net>
wrote:

>On Wed, 05 Sep 2012 17:46:49 -0400, Jim T. <x@y.z> wrote:
>
>>On Wed, 05 Sep 2012 13:53:26 -0700, Gene Wirchenko <genew@ocis.net>
>>wrote:
>>
>>>Dear JavaScripters:
>>>
>>>     Is there any way to pass parameters to the ready-state-change
>>>function that checks the progress of a request?  I am currently using
>>>a global.  I would prefer not to.
>
>[snip]
>
>>Something like this maybe?
>>
>>oRequest.onreadystatechange=
>>    function()
>>    {
>>        var state = 0;
>>        
>>        return function()
>>                   {
>>                      state++;
>>
>>                      if ( state == 1)
>>                          <process data for scenario 1>
>>                     else
>>                          <process data for scenario 2>
>>                  };
>>    } ();
>
>     Uh, no.  The "1" and "2" in the names were not meant to imply any
>order.
>
>     I am working on the general design for Web pages which would
>allow CRUD.  Depending on the request to be performed, the result back
>from the server will be handled differently.  Request types could be
>in any order.
>
>     I also prefer to have each scenario broken out into its own
>function.  IMHO, debugging Web code is mucky enough without having
>longer-than-necessary functions.
>
>Sincerely,
>
>Gene Wirchenko

The main point was to eliminate global variables by using a closure.
The other details are up to you.

Here's the same thing, abstracted to they way you originally stated it

oRequest.onreadystatechange=
    function()
    {
        <state variables>
        
        return function()
                   {
                        <state change handling>
                         ProcessData();                         
                   };
    } ();

[toc] | [prev] | [next] | [standalone]


#15730

FromJim T. <x@y.z>
Date2012-09-05 21:43 -0400
Message-ID<cpuf48d069jueeh1om9kfnklv4iruvr66m@4ax.com>
In reply to#15725
On Wed, 05 Sep 2012 13:53:26 -0700, Gene Wirchenko <genew@ocis.net>
wrote:

>Dear JavaScripters:
>
>     Is there any way to pass parameters to the ready-state-change
>function that checks the progress of a request?  I am currently using
>a global.  I would prefer not to.
>
>     What I want to pass is the name of a data-handling function to be
>executed if there is data returned.  To me, it would be neater to do:
>
>oRequest.onreadystatechange=StateChange somehow passing DataHandle1 or
>2 to StateChange() as parm ProcessData
>and
>function StateChange()
>  {
>  <state change handling>
>  ProcessData();     // Handle the data received.
>  }

I just realized this is for XMLHttpRequest. Sorry! Perhaps this is
closer to your needs...

oRequest.onreadystatechange=
  function ( xhr, handlers )
     {
       return function()
                   {
	      handlers[xhr.readyState]();
                   };
     }( oRequest, [ Handle1, Handle2, Handle3, Handle4 ]  );

[toc] | [prev] | [next] | [standalone]


#15731

FromJim T. <x@y.z>
Date2012-09-05 21:49 -0400
Message-ID<120g48lvc45826l9ng4d7enfmvvut5soke@4ax.com>
In reply to#15730
On Wed, 05 Sep 2012 21:43:27 -0400, Jim T. <x@y.z> wrote:

>On Wed, 05 Sep 2012 13:53:26 -0700, Gene Wirchenko <genew@ocis.net>
>wrote:
>
>>Dear JavaScripters:
>>
>>     Is there any way to pass parameters to the ready-state-change
>>function that checks the progress of a request?  I am currently using
>>a global.  I would prefer not to.
>>
>>     What I want to pass is the name of a data-handling function to be
>>executed if there is data returned.  To me, it would be neater to do:
>>
>>oRequest.onreadystatechange=StateChange somehow passing DataHandle1 or
>>2 to StateChange() as parm ProcessData
>>and
>>function StateChange()
>>  {
>>  <state change handling>
>>  ProcessData();     // Handle the data received.
>>  }
>
>I just realized this is for XMLHttpRequest. Sorry! Perhaps this is
>closer to your needs...
>
>oRequest.onreadystatechange=
>  function ( xhr, handlers )
>     {
>       return function()
>                   {
>	      handlers[xhr.readyState]();
>                   };
>     }( oRequest, [ Handle1, Handle2, Handle3, Handle4 ]  );
>
...should be handlers[xhr.readyState-1]();

And if you need to use this with other xhr objects you can change it
to this

oRequest.onreadystatechange=
   StateChange( oRequest, [ Handle1, Handle2, Handle3, Handle4 ]  );

function StateChange( xhr, handlers )
     {
       return function()
                   {
	      handlers[xhr.readyState-1]();
                   };
     };

[toc] | [prev] | [next] | [standalone]


#15747

FromStefan Weiss <krewecherl@gmail.com>
Date2012-09-06 18:56 +0200
Message-ID<k2akj4$bca$1@news.albasani.net>
In reply to#15725
On 2012-09-05 22:53, Gene Wirchenko wrote:
>      Is there any way to pass parameters to the ready-state-change
> function that checks the progress of a request?  I am currently using
> a global.  I would prefer not to.
> 
>      What I want to pass is the name of a data-handling function to be
> executed if there is data returned.

(I hope you don't actually want to pass the _name_ of the callback
function, but rather a reference to the function.)

I'm afraid I don't understand what the actual problem is. If you have
two data handler functions -

  function dataHandler1 (str) {
      alert("data handler 1 received: " + str);
  }

  function dataHandler2 (str) {
      alert("data handler 2 received: " + str);
  }

- and some generic function to do the XHR stuff -

  function fetch (url, callback) {
      // (minimal example)
      var xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.onreadystatechange = function () {
          if (xhr.readyState == "4") {
              callback(xhr.responseText);
          }
      };
      xhr.send();
  }

- then this is how you can direct the result to one of the two handlers:

  fetch("/a.txt", dataHandler1);
  fetch("/a.txt", dataHandler2);

What am I missing?


- stefan

[toc] | [prev] | [next] | [standalone]


#15748

FromGene Wirchenko <genew@ocis.net>
Date2012-09-06 10:55 -0700
Message-ID<qpoh48d7k9ngv2vubvmcck55m4gh4io1ij@4ax.com>
In reply to#15747
On Thu, 06 Sep 2012 18:56:04 +0200, Stefan Weiss
<krewecherl@gmail.com> wrote:

>On 2012-09-05 22:53, Gene Wirchenko wrote:
>>      Is there any way to pass parameters to the ready-state-change
>> function that checks the progress of a request?  I am currently using
>> a global.  I would prefer not to.
>> 
>>      What I want to pass is the name of a data-handling function to be
>> executed if there is data returned.
>
>(I hope you don't actually want to pass the _name_ of the callback
>function, but rather a reference to the function.)

     Yes.

>I'm afraid I don't understand what the actual problem is. If you have
>two data handler functions -

[snipped code]

>- and some generic function to do the XHR stuff -

[snipped code]

>- then this is how you can direct the result to one of the two handlers:
>
>  fetch("/a.txt", dataHandler1);
>  fetch("/a.txt", dataHandler2);
>
>What am I missing?

     Probably, it is just me missing how to structure my code.  I am
learning this all painfully a fraction of a step at a time. Sometimes,
I stumble.

     Thank you for the help.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#15749

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-09-06 21:38 +0200
Message-ID<10463793.jXXL31m3yW@PointedEars.de>
In reply to#15747
Stefan Weiss wrote:

> […]
>   function fetch (url, callback) {
>       // (minimal example)
>       var xhr = new XMLHttpRequest();
>       xhr.open("GET", url, true);
>       xhr.onreadystatechange = function () {
>           if (xhr.readyState == "4") {
>               callback(xhr.responseText);
>           }
>       };
>       xhr.send();
>   }
> […]
> 
> What am I missing?

At first glance, that the `readyState' property values are of type Number, 
and that xhr.send(null) should be called with GET.


PointedEars
-- 
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

[toc] | [prev] | [next] | [standalone]


#15750

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-09-06 21:49 +0200
Message-ID<1847393.YeQSDktdzs@PointedEars.de>
In reply to#15749
Thomas 'PointedEars' Lahn wrote:

> Stefan Weiss wrote:
>> […]
>>   function fetch (url, callback) {
>>       // (minimal example)
>>       var xhr = new XMLHttpRequest();
>>       xhr.open("GET", url, true);
>>       xhr.onreadystatechange = function () {
>>           if (xhr.readyState == "4") {
>>               callback(xhr.responseText);
>>           }
>>       };
>>       xhr.send();
>>   }
>> […]
>> 
>> What am I missing?
> 
> At first glance, that the `readyState' property values are of type Number,
> and that xhr.send(null) should be called with GET.

At second glance, that readyState == 4 does not need to mean success (you 
have to check the `status' as well) and that `callback' has no idea whether 
an empty `responseText' means success or failure.  I think that should be 
fixed even in a minimal XHR example:

  function fetch (url, callback)
  {
    /* (minimal example) */
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200)
      {
        callback(xhr.responseText);
      }
    };
    xhr.send(null);
  }

BTW, it is acceptable to see that you also use the space-before-parameter-
list convention.  It makes calls so much easier to find, does it not?


PointedEars
-- 
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

[toc] | [prev] | [next] | [standalone]


#15755

FromGene Wirchenko <genew@ocis.net>
Date2012-09-06 15:49 -0700
Message-ID<hs9i48l1r77mh36nj9jq6jofp9h837o0pu@4ax.com>
In reply to#15747
On Thu, 06 Sep 2012 18:56:04 +0200, Stefan Weiss
<krewecherl@gmail.com> wrote:

[snip]

>  function fetch (url, callback) {
>      // (minimal example)
>      var xhr = new XMLHttpRequest();
>      xhr.open("GET", url, true);
>      xhr.onreadystatechange = function () {
>          if (xhr.readyState == "4") {
>              callback(xhr.responseText);
>          }
>      };
>      xhr.send();
>  }
>
>- then this is how you can direct the result to one of the two handlers:
>
>  fetch("/a.txt", dataHandler1);
>  fetch("/a.txt", dataHandler2);
>
>What am I missing?

     Once again, it is me missing things.

     OK, I have worked with this some more.

     I need to handle errors.  If I understand correctly, I am forced
to do that in the state change function or something called by it.  So
what you call callback will have to have this handling.  I will have
to adjust the input form status so that the user is not locked out
from work should an error occur.  This seems rather awkward (nasty
coupling).  I hope I can avoid it, but it seems that is exactly what I
will have to do.  Is that correct?

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#15759

FromTim Streater <timstreater@greenbee.net>
Date2012-09-07 09:15 +0100
Message-ID<timstreater-90133A.09150507092012@news.individual.net>
In reply to#15755
In article <hs9i48l1r77mh36nj9jq6jofp9h837o0pu@4ax.com>,
 Gene Wirchenko <genew@ocis.net> wrote:

> On Thu, 06 Sep 2012 18:56:04 +0200, Stefan Weiss
> <krewecherl@gmail.com> wrote:
> 
> [snip]
> 
> >  function fetch (url, callback) {
> >      // (minimal example)
> >      var xhr = new XMLHttpRequest();
> >      xhr.open("GET", url, true);
> >      xhr.onreadystatechange = function () {
> >          if (xhr.readyState == "4") {
> >              callback(xhr.responseText);
> >          }
> >      };
> >      xhr.send();
> >  }
> >
> >- then this is how you can direct the result to one of the two handlers:
> >
> >  fetch("/a.txt", dataHandler1);
> >  fetch("/a.txt", dataHandler2);
> >
> >What am I missing?
> 
>      Once again, it is me missing things.
> 
>      OK, I have worked with this some more.
> 
>      I need to handle errors.

What sort of errors are you referring to? Do you mean xhr failures or do 
you mean errors you have detected and are reporting back vis the ajax 
mechanism?

> If I understand correctly, I am forced
> to do that in the state change function or something called by it.  So
> what you call callback will have to have this handling.

Of course. That's where the processing of the responseText takes place.

By the way, as has been pointed out you need to check that status==200 
also. You also need to set xhr = null when you've finished with it.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#15774

FromGene Wirchenko <genew@ocis.net>
Date2012-09-07 09:58 -0700
Message-ID<mf9k489u8j684s2unrfbaaq62idmti1dsi@4ax.com>
In reply to#15759
On Fri, 07 Sep 2012 09:15:05 +0100, Tim Streater
<timstreater@greenbee.net> wrote:

>In article <hs9i48l1r77mh36nj9jq6jofp9h837o0pu@4ax.com>,
> Gene Wirchenko <genew@ocis.net> wrote:
>
>> On Thu, 06 Sep 2012 18:56:04 +0200, Stefan Weiss
>> <krewecherl@gmail.com> wrote:

[snip]

>> >What am I missing?
>> 
>>      Once again, it is me missing things.
>> 
>>      OK, I have worked with this some more.
>> 
>>      I need to handle errors.
>
>What sort of errors are you referring to? Do you mean xhr failures or do 
>you mean errors you have detected and are reporting back vis the ajax 
>mechanism?

     Both though I consider them separate issues.

     XHR failures: I need to handle the XHR errors correctly.  What I
have it as right now is
          1xx: Just return from in state change function.
          200, 306: Treat as success and process the returned data.
          otherwise: Report error, and reset the form status to
something appropriate.

     The handling routine was originally intended to handle just the
errors reported in the returned data.  That is more complicated, but
it is also within what I know for sure how to deal with.

>> If I understand correctly, I am forced
>> to do that in the state change function or something called by it.  So
>> what you call callback will have to have this handling.
>
>Of course. That's where the processing of the responseText takes place.

     I thought that is probably the case, but I make enough mistakes
out of not knowing that I prefer to ask check questions.
>
>By the way, as has been pointed out you need to check that status==200 
>also. You also need to set xhr = null when you've finished with it.

     I know about 200.

     Setting xhr to null?  Why?  I may well be using it again.  The
forms are CRUD and so there may be multiple transactions go to the
server.

     Thank you for the help.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#15776

FromTim Streater <timstreater@greenbee.net>
Date2012-09-07 19:14 +0100
Message-ID<timstreater-DBAFA6.19140107092012@news.individual.net>
In reply to#15774
In article <mf9k489u8j684s2unrfbaaq62idmti1dsi@4ax.com>,
 Gene Wirchenko <genew@ocis.net> wrote:

> >By the way, as has been pointed out you need to check that status==200 
> >also. You also need to set xhr = null when you've finished with it.
> 
>      I know about 200.
> 
>      Setting xhr to null?  Why?  I may well be using it again.  The
> forms are CRUD and so there may be multiple transactions go to the
> server.

And each time you call your ajax function you'll get a new one. AIUI, 
although my knowledge of closures is a bit limited, each one will live 
forever unless you explicitly dump it when you've finished with it. By 
setting it to null, it'll get garbage-collected subsequently.

Here's what I do (slightly simplified, as I log the status error rather 
than just alert-ing):

function ajax (url, data, callbackFunction)
     {

     var request = window.XMLHttpRequest ? new XMLHttpRequest () : new 
ActiveXObject ("MSXML2.XMLHTTP.3.0");

     request.open ("post", url, true);
     request.setRequestHeader ("Content-Type", 
"application/x-www-form-urlencoded; charset=utf-8"); 
 
     request.onreadystatechange = function ()
          {

          if  (request.readyState!=4)  return;

          if  (request.status==200 && request.responseText!="")  
callbackFunction (request.responseText);
          if  (request.status!=200)  alert ("Bad ajax status: " + 
request.status);

          request = null;

          };

     request.send (data);

     }

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#15783

Fromcezary.tomczyk@gmail.com
Date2012-09-07 13:51 -0700
Message-ID<ee771d80-df4b-4b4a-b5ee-e2cc65a178ad@googlegroups.com>
In reply to#15776
W dniu piątek, 7 września 2012 20:14:03 UTC+2 użytkownik Tim Streater napisał:
[...]
>           if  (request.status==200 && request.responseText!="")  
[...]

Is the status of the 304 should also be included here?

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

[toc] | [prev] | [next] | [standalone]


#15787

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-09-08 00:52 +0200
Message-ID<2913825.zHrt7aGx1u@PointedEars.de>
In reply to#15783
cezary.tomczyk@gmail.com wrote:

> W dniu piątek, 7 września 2012 20:14:03 UTC+2 użytkownik Tim Streater
> napisał:

It's an attribution _line_, not an attribution novel.

> [...]
>>           if  (request.status==200 && request.responseText!="")
> [...]
> 
> Is the status of the 304 should also be included here?

If it would occur.  So far, all XHR implementations handle caching and 
redirections transparently.


PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

[toc] | [prev] | [next] | [standalone]


#15789

Fromcezary.tomczyk@gmail.com
Date2012-09-07 16:32 -0700
Message-ID<e22caa6d-0b21-46ea-bf5f-2db6f561e9ca@googlegroups.com>
In reply to#15787
W dniu sobota, 8 września 2012 00:53:24 UTC+2 użytkownik Thomas 'PointedEars' Lahn napisał:
> cezary.tomczyk@gmail.com wrote:
> 
> 
> 
> > W dniu piątek, 7 września 2012 20:14:03 UTC+2 użytkownik Tim Streater
> 
> > napisał:
> 
> 
> 
> It's an attribution _line_, not an attribution novel.
> 
> 
> 
> > [...]
> 
> >>           if  (request.status==200 && request.responseText!="")
> 
> > [...]
> 
> > 
> 
> > Is the status of the 304 should also be included here?
> 
> 
> 
> If it would occur.  So far, all XHR implementations handle caching and 
> 
> redirections transparently.

So, if server will response with 304 header then XHR will return status 200 or 304? Because if all XHR implementations handle caching transparently then just checking only status == 200 is not a good idea. Or I something missed.

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

[toc] | [prev] | [next] | [standalone]


#15792

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-09-08 09:48 +0200
Message-ID<1995887.BCnZExLFZj@PointedEars.de>
In reply to#15789
cezary.tomczyk@gmail.com wrote:
^^^^^^^^^^^^^^^^^^^^^^^^

> W dniu sobota, 8 września 2012 00:53:24 UTC+2 użytkownik Thomas
> 'PointedEars' Lahn napisał:

Will you *please* stop posting attribution novels?  And get a real name.

>> cezary.tomczyk@gmail.com wrote:
>> >>           if  (request.status==200 && request.responseText!="")
>> > [...]
>> > 
>> > Is the status of the 304 should also be included here?
>> 
>> If it would occur.  So far, all XHR implementations handle caching and
   ^^
>> redirections transparently.
> 
> So, if server will response with 304 header then XHR will return status
> 200 or 304?

200, and the resource is retrieved from the cache.  Why don't you try?

> Because if all XHR implementations handle caching transparently then just
> checking only status == 200 is not a good idea. Or I something missed.

AFAIK, to say something is "transparent" to the user means the user does not 
need not be aware of it.  For example, you use a "transparent (HTTP) proxy" 
without having to know it even exists.

Please do not Cc: me.  I am subscribed to the newsgroups I am posting to.
And do not use Google Groups for posting, it is completely borken.


PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

[toc] | [prev] | [next] | [standalone]


#15793

FromCezary Tomczyk <cezary.tomczyk@gmail.com>
Date2012-09-08 02:09 -0700
Message-ID<c79119fd-d4a8-462c-a3d0-184abfbe203d@googlegroups.com>
In reply to#15792
W dniu sobota, 8 września 2012 09:49:13 UTC+2 użytkownik Thomas 'PointedEars' Lahn napisał:
> cezary.tomczyk@gmail.com wrote:
> 
> ^^^^^^^^^^^^^^^^^^^^^^^^
[...]
> Will you *please* stop posting attribution novels?  And get a real name.

As for real name, I changed settings and now should be full name instead of just e-mail. However, my e-mail contains my full name and I thought that this should be enough.

> >> cezary.tomczyk@gmail.com wrote:
> 
> >> >>           if  (request.status==200 && request.responseText!="")
> 
> >> > [...]
> 
> >> > 
> 
> >> > Is the status of the 304 should also be included here?
> 
> >> 
> 
> >> If it would occur.  So far, all XHR implementations handle caching and
> 
>    ^^
> 
> >> redirections transparently.
> 
> > 
> 
> > So, if server will response with 304 header then XHR will return status
> 
> > 200 or 304?
> 
> 
> 
> 200, and the resource is retrieved from the cache.  Why don't you try?

I checked and its seems that there is always status 200. You where right.

> > Because if all XHR implementations handle caching transparently then just
> 
> > checking only status == 200 is not a good idea. Or I something missed.
> 
> 
> 
> AFAIK, to say something is "transparent" to the user means the user does not 
> 
> need not be aware of it.  For example, you use a "transparent (HTTP) proxy" 
> 
> without having to know it even exists.

Makes sense.

> Please do not Cc: me.  I am subscribed to the newsgroups I am posting to.

Sorry, I didn't saw that option and now I see that Google Groups check this option by default.

> And do not use Google Groups for posting, it is completely borken.

NNTP server which I used in the past is now down. That's why I use Google Groups. I need to find new NNTP server which allow me not only reading, but posting too.

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

[toc] | [prev] | [next] | [standalone]


#15795

FromTim Streater <timstreater@greenbee.net>
Date2012-09-08 11:09 +0100
Message-ID<timstreater-9185CA.11090008092012@news.individual.net>
In reply to#15789
In article <e22caa6d-0b21-46ea-bf5f-2db6f561e9ca@googlegroups.com>,
 cezary.tomczyk@gmail.com wrote:

> So, if server will response with 304 header then XHR will return status 200 
> or 304? Because if all XHR implementations handle caching transparently then 
> just checking only status == 200 is not a good idea. Or I something missed.

I've had status==0, but that was probably a Safari 5.0 bug (about which 
I had discussions with Apple Engineering and more recent versions have 
not suffered from).

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.javascript


csiph-web