-
11 Jun 2012 12:00 PM #1
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'); } });
-
Best Answer Posted by montymccune
If anyone else is having this same problem, I solved my issue with the following method:
...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.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; }
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' },
-
13 Jun 2012 8:27 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,684
- Vote Rating
- 435
- Answers
- 3111
will return something like:Code:Ext.Date.format(new Date(), 'MS')
Which the "\" is escaping "/" so when this gets JSON encoded with this:Code:\/Date(1339604509173)\/
it will return what you are seeing:Code:Ext.encode(Ext.Date.format(new Date(), 'MS'))
This is how Javascript works as the same thing happens without using the ST JSON encoding (Ext.encode):Code:"\\/Date(1339604550698)\\/"
Code:JSON.stringify(Ext.Date.format(new Date(), 'MS'))
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
13 Jun 2012 10:07 AM #3
Problem solved...
Problem solved...
If anyone else is having this same problem, I solved my issue with the following method:
...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.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; }
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' },
-
24 Aug 2012 5:52 AM #4
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;
}
});


Reply With Quote