1. #131
    Sencha - Community Support Team hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,963
    Vote Rating
    10
    hendricd will become famous soon enough hendricd will become famous soon enough

      0  

    Default


    Quote Originally Posted by mubenchi View Post
    i want to use Ext.lib.Ajax.onStatus to deal with the situation that the server check session timeout,and give a http 302 or 401 and so on status.could any one give me a help?thanks
    @mubenchi -- Have a look at this post (and beyond, on this thread) for some sample usage and other things you may need to consider as well.
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


  2. #132
    Sencha - Community Support Team hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,963
    Vote Rating
    10
    hendricd will become famous soon enough hendricd will become famous soon enough

      0  

    Default responseJSON, the needs of the many....

    responseJSON, the needs of the many....


    Quote Originally Posted by ZooKeeper View Post
    I'm also greatly in favor of responseJson. It'll be a lifesaver, since I have to decode it in EVERY callback.
    Relying that much on content type is probably not such a good idea, since not everyone can set it.
    @Zookeeper -- While I understand where you're coming from with that, it's somewhat dangerous to be evaling anything that comes along in a response.

    Anywho:

    I've given all this a bit of thought (during a short vacation )...

    There are a number of issues that arise when making assumptions about JSON handling, and I thought I would throw out a code frag for review/feedback for sensibility:

    Code:
    Ext.apply(Ext.lib.Ajax, {
    
      decodeJSON : Ext.decode,  //optional custom JSON decoder implementation
    
      reCtypeJSON : /\/json/gi , 
    
      createResponseObject : function(o, callbackArg, isAbort){
            var obj = {responseXML   :null,
                       responseText  :'',
                       responseStream : null,
                       responseJSON  : null,
                       getResponseHeader : {},
                       getAllResponseHeaders : ''
                       };
    
             // some existing code omitted for clarity......
    
              if(o.options.isJSON || this.reCtypeJSON.test( headerObj['Content-Type'] || "" ){
                 try{
                      obj.responseJSON = this.decodeJSON(obj.responseText);
                   } catch(exJSON){ 
                      o.status.isError = true;
                      o.status.error = exJSON;
                     //Raise lib.Ajax Exception Event 
                      if(this.events){
                         this.fireEvent('exception',o ,obj, callback, isAbort, exJSON);
                      }
                     
                   }
              }
              .........
      });
    Some notes on flexibility:

    1) It comes at a price. Content-Type header evaluation could be expensive.
    2) Script injection is a risk here, so an optional Ext.lib.Ajax.decodeJSON function permits custom JSON "Crockford-scrutiny" if your implementation demands it (otherwise it falls back to the standard Ext.decode -- a simple eval).
    3) The regex Content-Type test pattern (reCtypeJSON ) can be adjusted to suite your needs as well.
    4) request.options.isJSON is a request hint that the response will be JSON, thus it would preclude the need to adapt server code to send either text/json or application/json Content-type response headers on each response (altho in practice, the new RFC says you SHOULD).

    Food for thought.
    Last edited by hendricd; 17 Jul 2008 at 3:06 AM. Reason: added decodeJSON default and exception handling
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


  3. #133
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    might be easier to just set
    Code:
    decodeJSON: Ext.decode, // custom json decoder function -- defaults to Ext.decode
    then you could do away with
    Code:
    this.decodeJSON  || (this.decodeJSON = Ext.decode);
    other than that, it looks good -- as usual

  4. #134
    Sencha User
    Join Date
    Mar 2007
    Posts
    49
    Vote Rating
    0
    Digital God is on a distinguished road

      0  

    Default


    hendricd
    i'm using DOM method, because i use Adobe AIR and i can't use eval function

  5. #135
    Ext JS Premium Member
    Join Date
    Jul 2007
    Posts
    218
    Vote Rating
    2
    ZooKeeper is on a distinguished road

      0  

    Default


    Quote Originally Posted by mystix View Post
    might be easier to just set
    Code:
    decodeJSON: Ext.decode, // custom json decoder function -- defaults to Ext.decode
    then you could do away with
    Code:
    this.decodeJSON  || (this.decodeJSON = Ext.decode);
    other than that, it looks good -- as usual
    I cann't agree more. If you are expecting Json then if it decode fails then it's either not a 200 code or something else which is an error anyway (there should be a param showing that json was successfully parsed). The other issue is if you want to figure out what error was it, parsing error, timeout or non200 code.

    For myself I can say that I'd be happy with decodeJSON param and flag that it was successfully parsed or not.

  6. #136
    Sencha - Community Support Team hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,963
    Vote Rating
    10
    hendricd will become famous soon enough hendricd will become famous soon enough

      0  

    Default


    See Code sample revised -- Thx Mystix.

    Quote Originally Posted by ZooKeeper View Post
    I cann't agree more. If you are expecting Json then if it decode fails then it's either not a 200 code or something else which is an error anyway (there should be a param showing that json was successfully parsed). The other issue is if you want to figure out what error was it, parsing error, timeout or non200 code.

    For myself I can say that I'd be happy with decodeJSON param and flag that it was successfully parsed or not.
    @ZooKeeper -- HTTP status and timeout are already handled elsewhere in the basex stack.
    See the revised code that adds the exception event when JSON decoding fails.

    Also, if(response.responseJSON !== null) it processed a good JSON response.
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


  7. #137
    Sencha - Community Support Team hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,963
    Vote Rating
    10
    hendricd will become famous soon enough hendricd will become famous soon enough

      0  

    Default responseJSON revisions...

    responseJSON revisions...


    posted to svn/trunk.

    Lemme know how it goes.
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


  8. #138
    Sencha - Community Support Team hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,963
    Vote Rating
    10
    hendricd will become famous soon enough hendricd will become famous soon enough

      0  

    Default


    Quote Originally Posted by Digital God View Post
    hendricd
    i'm using DOM method, because i use Adobe AIR and i can't use eval function
    @Digital God -- I'm not clear on what you are attempting here?

    Are you trying to load a text file (what?) with a <script> tag(method:'DOM')? You don't have any control over response character-set from within the browser. The server must comply with your encoding wishes.
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


  9. #139
    Ext JS Premium Member
    Join Date
    Jul 2007
    Posts
    218
    Vote Rating
    2
    ZooKeeper is on a distinguished road

      0  

    Default


    There's a parentesis missing on the line
    if(o.options.isJSON || (this.reCtypeJSON && this.reCtypeJSON.test( headerObj['Content-Type'] || "" ) )){

  10. #140
    Sencha User
    Join Date
    Mar 2007
    Posts
    49
    Vote Rating
    0
    Digital God is on a distinguished road

      0  

    Default


    2hendricd
    forget it, i've found my mistake))

Thread Participants: 82

  1. galdaka (1 Post)
  2. Digital God (3 Posts)
  3. prophet (1 Post)
  4. alayasf (2 Posts)
  5. tof (1 Post)
  6. stever (3 Posts)
  7. mystix (4 Posts)
  8. fangzhouxing (6 Posts)
  9. ludoo (1 Post)
  10. KRavEN (4 Posts)
  11. DigitalSkyline (3 Posts)
  12. daemach (1 Post)
  13. violinista (1 Post)
  14. mlarese (1 Post)
  15. walker_cn (4 Posts)
  16. stevebla (4 Posts)
  17. Chris in Cambridge (1 Post)
  18. mjlecomte (2 Posts)
  19. ZooKeeper (10 Posts)
  20. brookd (1 Post)
  21. fermo111 (6 Posts)
  22. kevinwu8 (1 Post)
  23. Spirit (3 Posts)
  24. jerrybrown5 (4 Posts)
  25. lvanderree (8 Posts)
  26. Makor (4 Posts)
  27. yanick (1 Post)
  28. wm003 (10 Posts)
  29. radio1 (3 Posts)
  30. jphillips (2 Posts)
  31. markpele (2 Posts)
  32. Romantik (1 Post)
  33. Sander_S (4 Posts)
  34. maximumcoder (2 Posts)
  35. NotChris (6 Posts)
  36. ritchrs (2 Posts)
  37. sksoft (2 Posts)
  38. jvanantwerp (2 Posts)
  39. jfa (2 Posts)
  40. BlueCamel (1 Post)
  41. ClemsonJeeper (3 Posts)
  42. sp797 (1 Post)
  43. mahesh122 (2 Posts)
  44. zombeerose (12 Posts)
  45. jlowe (2 Posts)
  46. bt_bruno (2 Posts)
  47. pompom (4 Posts)
  48. Eric24 (18 Posts)
  49. baumschule (3 Posts)
  50. coderobo (3 Posts)
  51. krause (1 Post)
  52. mubenchi (1 Post)
  53. mrkadakia (3 Posts)
  54. tomim (4 Posts)
  55. canxss (2 Posts)
  56. tasman (3 Posts)
  57. mrsunshine (4 Posts)
  58. Trinad (2 Posts)
  59. jasonb885 (2 Posts)
  60. wki01 (5 Posts)
  61. mschwartz (2 Posts)
  62. sarathy (2 Posts)
  63. jdurante (1 Post)
  64. TomChiverton (1 Post)
  65. sumit.madan (2 Posts)
  66. Riemi (4 Posts)
  67. Henrik Rutzou (3 Posts)
  68. hairinwind (1 Post)
  69. gzlizp (1 Post)
  70. yuanqixun (2 Posts)
  71. larryaubstore (2 Posts)
  72. wupeng (2 Posts)
  73. blueram (1 Post)
  74. ronivcp (1 Post)
  75. t00bs (2 Posts)
  76. hyponym (4 Posts)
  77. mg2468 (3 Posts)
  78. scaddenp (1 Post)
  79. faith wins (3 Posts)
  80. aratcliffe (8 Posts)
  81. jhashe (3 Posts)
  82. jaufgang (1 Post)

Tags for this Thread