1. #1
    Sencha Premium Member
    Join Date
    Apr 2012
    Posts
    27
    Answers
    1
    Vote Rating
    0
    montymccune is on a distinguished road

      0  

    Default Answered: JSON encoding ESCAPES MS DATE format

    Answered: JSON encoding ESCAPES MS DATE format


    I am trying to submit a Date to a Microsoft REST service that requires the \/Date()\/ format. However whenever the Ajax request converts my JSON object to string it adds additional \ to the date format resulting in \\/Date()\\/. How can I avoid this? Here is my code:



    Code:
            var tRx = {
                'scriptBundle':[
                    {
                        'DispenseAs': record.data.dispenseAs,
                        'IsCompound': record.data.isCompound,
                        'RX_QTY': record.data.qty,
                        'Refills': record.data.refills,
                        'ProviderID': record.data.providerID,
                        'RX_Description': record.data.ndc,
                        'RX_Sig': record.data.sig,
                        'RX_Type': record.data.type,
                        'RX_Dosage': record.data.dosage,
                        'RX_Form': record.data.form,
                        'RX_Name': record.data.name,
                        'RX_Timing': record.data.timing,
                        'RX_Duration': record.data.duration,
                        'RX_Notes': record.data.notes,
                        'RX_Route': record.data.route,
                        'RX_Strength': record.data.strength,
                        'PatientID': tPat.PatientID,
                        'UserID': tUser.UserID,
                        'LastRefill': Ext.Date.format(tDate,'MS'),
                        //'RX_Date': Ext.Date.format(tDate,'MS'),
                        'RX_Status': 'Active'
                    }
                ]
            }
    
            Ext.Ajax.request({
                url: phxUtility.getServerAddress() + '/server/rx.svc/rx/save',
                method: 'POST',
                jsonData: tRx,
                headers:{
                    'Authorization': 'Basic ' + Base64.encode(tUser.UserName + ':' + tUser.password)
                },
                success: function(response, opts) {
    
    
                    //refresh dataview
                    me.loadRxData();
    
    
                },
                failure: function(response, opts) {
                    console.log('server-side failure with status code ' + response.status);
                },
                callback: function(options, success, response){
                    console.log('callback');
                }
            });

  2. Quote Originally Posted by mitchellsimoens View Post
    Code:
    Ext.Date.format(new Date(), 'MS')
    will return something like:

    Code:
    \/Date(1339604509173)\/
    Which the "\" is escaping "/" so when this gets JSON encoded with this:

    Code:
    Ext.encode(Ext.Date.format(new Date(), 'MS'))
    it will return what you are seeing:

    Code:
    "\\/Date(1339604550698)\\/"
    This is how Javascript works as the same thing happens without using the ST JSON encoding (Ext.encode):

    Code:
    JSON.stringify(Ext.Date.format(new Date(), 'MS'))
    If anyone else is having this same problem, I solved my issue with the following method:

    Code:
        addDate: function(jsonString, dateDescription, date){
            var msDate = Ext.Date.format(date,'MS');
            var tLIndex = jsonString.lastIndexOf("]");
            tLIndex--;
            var finalString = jsonString.substr(0, tLIndex);
    
    
            //add date
            finalString = finalString.concat(',"' + dateDescription + '":"' + msDate + '"');
    
    
            //add closing brakets
            finalString = finalString.concat('}]}');
            return finalString;
        }
    ...basically you pass in the JSON encoded string, the date property description, and a date object. It returns a new JSON encoded string with the date appended to the end.

    Also I had to IGNORE the jsonData object in the Ajax.request and instead I used the params object to hold the JSON encoded string. Finally I had to add the Content-Type to the headers like so:

    Code:
    
                headers:{
                    'Content-Type': 'application/json; charset=utf-8'
                },

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,118
    Answers
    3504
    Vote Rating
    855
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Code:
    Ext.Date.format(new Date(), 'MS')
    will return something like:

    Code:
    \/Date(1339604509173)\/
    Which the "\" is escaping "/" so when this gets JSON encoded with this:

    Code:
    Ext.encode(Ext.Date.format(new Date(), 'MS'))
    it will return what you are seeing:

    Code:
    "\\/Date(1339604550698)\\/"
    This is how Javascript works as the same thing happens without using the ST JSON encoding (Ext.encode):

    Code:
    JSON.stringify(Ext.Date.format(new Date(), 'MS'))
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha Premium Member
    Join Date
    Apr 2012
    Posts
    27
    Answers
    1
    Vote Rating
    0
    montymccune is on a distinguished road

      0  

    Default Problem solved...

    Problem solved...


    Quote Originally Posted by mitchellsimoens View Post
    Code:
    Ext.Date.format(new Date(), 'MS')
    will return something like:

    Code:
    \/Date(1339604509173)\/
    Which the "\" is escaping "/" so when this gets JSON encoded with this:

    Code:
    Ext.encode(Ext.Date.format(new Date(), 'MS'))
    it will return what you are seeing:

    Code:
    "\\/Date(1339604550698)\\/"
    This is how Javascript works as the same thing happens without using the ST JSON encoding (Ext.encode):

    Code:
    JSON.stringify(Ext.Date.format(new Date(), 'MS'))
    If anyone else is having this same problem, I solved my issue with the following method:

    Code:
        addDate: function(jsonString, dateDescription, date){
            var msDate = Ext.Date.format(date,'MS');
            var tLIndex = jsonString.lastIndexOf("]");
            tLIndex--;
            var finalString = jsonString.substr(0, tLIndex);
    
    
            //add date
            finalString = finalString.concat(',"' + dateDescription + '":"' + msDate + '"');
    
    
            //add closing brakets
            finalString = finalString.concat('}]}');
            return finalString;
        }
    ...basically you pass in the JSON encoded string, the date property description, and a date object. It returns a new JSON encoded string with the date appended to the end.

    Also I had to IGNORE the jsonData object in the Ajax.request and instead I used the params object to hold the JSON encoded string. Finally I had to add the Content-Type to the headers like so:

    Code:
    
                headers:{
                    'Content-Type': 'application/json; charset=utf-8'
                },

  5. #4
    Sencha User
    Join Date
    Jul 2009
    Posts
    3
    Vote Rating
    0
    apalchaudhry is on a distinguished road

      0  

    Default


    I suppose this will help ExtJS 4.1

    Ext.override(Ext.form.field.Date, {
    valueToRaw: function (value) {

    var val = this.formatDate(this.parseDate(value));

    if (!val) {
    var re = new RegExp('\/Date\\(([-+])?(\\d+)(?:[+-]\\d{4})?\\)\/'),
    val = (value || '').match(re);
    val = val ? new Date(((val[1] || '') + val[2]) * 1) : null;
    return this.formatDate(this.parseDate(val));
    }
    else
    return val;

    }
    });