Results 1 to 4 of 4

Thread: Get local time using Ajax request from web service

  1. #1
    Sencha User mkariti's Avatar
    Join Date
    Aug 2011
    Posts
    92

    Default Get local time using Ajax request from web service

    Hi there,
    I'm trying to send a n Ajax request to this website: http://www.earthtools.org/webservices.htm using the following code:
    Code:
                       Ext.regModel('Timezone', {
                           fields: ['localtime', 'isotime', 'utctime', 'location', 'offset', 'suffix', 'dst']
                       });
    
                       var store = new Ext.data.Store({
                           model: 'Timezone',
                           autoLoad: true,
                           proxy: {
                               type: 'ajax',
                               url: 'http://www.earthtools.org/timezone/40.71417/-74.00639',
                               reader: {
                                   type: 'xml',
                                   model: 'Timezone',
                                   record: 'timezone'
                               }
                           }
                       });
    For some reason I cant get an answer although it generates an XML response:
    http://www.earthtools.org/timezone/40.71417/-74.00639
    Also, where or how can I catch the response?
    I would like to present it to the user.
    Many thanks
    mkariti

  2. #2
    Sencha User mkariti's Avatar
    Join Date
    Aug 2011
    Posts
    92

    Default

    Ok so after some tests I found out that Sencha's XML request dont accept the following:

    Code:
    <timezone xsi:noNamespaceSchemaLocation="http://www.earthtools.org/timezone-1.1.xsd">
    <version>1.1</version>
    ?
    <location>
    <latitude>40.71417</latitude>
    <longitude>-74.00639</longitude>
    </location>
    <offset>-5</offset>
    <suffix>R</suffix>
    <localtime>30 Aug 2011 00:33:11</localtime>
    <isotime>2011-08-30 00:33:11 -0500</isotime>
    <utctime>2011-08-30 05:33:11</utctime>
    <dst>Unknown</dst>
    </timezone>
    To be more specific, it does not accept the first line of the XML respones!
    Any chance anyone know how to bypass it?
    Many thanks

  3. #3
    Sencha User
    Join Date
    Jul 2010
    Location
    Switzerland
    Posts
    78

    Default

    Write a custom xml reader, like this:
    Code:
    app.readers.customreader = Ext.extend(Ext.data.XmlReader, {
    
        readRecords: function(doc) {
            if (Ext.isArray(doc)) {
                doc = doc[0];
            }
        
            /**
             * manipulate your xml here,
             * e.g. strip the offending tags.
             * Use Ext.DomQuery.selectNode
             */
    
            return app.readers.customreader.superclass.readRecords.call(this, doc);
        }
    
    });
    
    Ext.data.ReaderMgr.registerType('customreader', app.readers.customreader);
    But this is really just a work around, there might be other things you can tweak.

  4. #4
    Sencha User mkariti's Avatar
    Join Date
    Aug 2011
    Posts
    92

    Default

    Hi fx-mike,
    Many thanks for your reply.
    I decided to use a small "web service" so to manipulate the parameters before returning the data to Sencha.
    I'm not sure it's a good Idea but I will go with it at least until I'll decide that its not working for me.
    Just if you are interested below is how I've manipulated the data:
    Code:
            WebRequest request = WebRequest.Create("http://www.earthtools.org/sun/" + latitude + "/" + longitude + "/" + date + "/" + timezone + "/" + dst + "");
    
            StreamReader stream = new StreamReader(request.GetResponse().GetResponseStream());
            StringBuilder builder = new StringBuilder();
            builder.Append(stream.ReadToEnd());
            builder.Replace("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>", "<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            builder.Replace("<timezone xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.earthtools.org/timezone-1.1.xsd\">", "<timezone>");
    Many thanks
    mkariti

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •