PDA

View Full Version : JSON writer not working as expected



stoertebeker
5 Jul 2012, 11:07 AM
Hi,
I have defined a proxy on a model and then use this in a store. The proxy looks like this:


proxy: {
type: 'jsonp',
url: 'checkout.php',
reader: {
type: 'json',
root: 'cart'
},
writer: {
type: 'json',
root: 'cart',
encode: 'true'
}

Now when I do a store.sync(), data is sent to the server and calls the php.script but the way the data looks is not as expected.
1. the record(s) are provided in the $_GET array in a linear structure
2. the root I have set appears nowhere
3. the default root 'records' show up as a key value in $_GET and has the key of the first field as a value like this: 'records:firstfield=value_of_firstfield'; then the other fields follow in the array with their respective values

I was actually expecting a JSON-like string handed over (in what form I do not know).

Any enlightening appreciated.

scottmartin
5 Jul 2012, 11:39 AM
If you remove the encode: true, what do you see in the header request payload?

Scott.

stoertebeker
5 Jul 2012, 11:55 AM
Hi Scott,
thanks for the quick reply. Since I am not the big expert, I hope i interpret your request correctly.
This is what is contained in the header:

Accept:
*/*
Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:
gzip,deflate,sdch
Accept-Language:
en-US,en;q=0.8
Connection:
keep-alive
Cookie:
fc=fcVal=6148734254643518848; PHPSESSID=jrc6bu2rg402h54kuj6hpaubq3
Host:
www.....com
Referer:
http://localhost/~yy/ext/xx/app.html
User-Agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11

The passed parameters look the same as before.

scottmartin
5 Jul 2012, 12:14 PM
Sorry ... I was looking for the sent data.

Scott.

stoertebeker
5 Jul 2012, 2:17 PM
that is the send data.

scottmartin
5 Jul 2012, 2:26 PM
hmm.. what debugger are you using? DeveloperTools in chrome?
You should see Request Payload section ..

example:


{"data":[{"id_request":297,"ticket_number":"R11668-B71028AA","id_project":1, ...}] }


if not .. then what did you mean by:

data is sent to the server and calls the php.script but the way the data looks is not as expected.

Where do you see this info is sent?


Scott.

stoertebeker
5 Jul 2012, 2:46 PM
Hi,
yes i am using the Chrome dev tools. so what i mean is the under the Network tab i click on the entry that results from the store.sync() call and there i see 5 tabs. from those i look at Headers.
Within Headers I have the Request-URL and Request Headers, Query String Parameters and Response Headers. What I sent earlier was from Request Headers.
The word Payloag doesnt appear anywhere.

stoertebeker
6 Jul 2012, 2:42 AM
Hi Scott,
I have rebuild the situation in a much simpler way in order to reproduce the issue and make the problem clearer. Here is the code:
Model

Ext.define('MyApp.model.MyModel', { extend: 'Ext.data.Model',

fields: [
{
name: 'f1'
},
{
name: 'f2'
},
{
name: 'f3'
}
],

proxy: {
type: 'jsonp',
url: '../checkout.php',
reader: {
type: 'json'
},
writer: {
type: 'json'
}
}
});

Store

Ext.define('MyApp.store.MyStore', { extend: 'Ext.data.Store',
requires: [
'MyApp.model.MyModel'
],


constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
storeId: 'MyStore',
model: 'MyApp.model.MyModel',
data: [
{
f1: 'a',
f2: 'b',
f3: 'c'
}
]
}, cfg)]);
}
});

Handler

onButtonClick: function(button, e, options) { var mystore = Ext.data.StoreManager.lookup('MyStore');
mystore.sync();
}
});

After mystore.sync(), this is what is sent to the server (Query String Parameters):



_dc:
1341570659973
records:
f1
f2:
b
f3:
c
callback:
Ext.data.JsonP.callback1

I was expecting something like this:
records: {f1:'a',f2:'b',f3:'c'}

I hope this helps to give me a hint where I have missed something.

Thanks Olaf.

redraid
6 Jul 2012, 3:09 AM
Try add to writer config "allowSingle: false"

writer: {
type: 'json',
allowSingle: false
}

stoertebeker
6 Jul 2012, 4:17 AM
Hi,result is unchanged, i.e. same as before.

scottmartin
21 Jul 2012, 1:57 PM
I noticed you are using JSONP .. is this a cross domain request? Hence the callback message
From your url, it seems like it is not .. try the following



proxy: {
type: 'ajax',
url: 'data.php',
reader: {
type: 'json',
root: 'data'
},
writer: {
type: 'json',
root: 'data'
}
}


Scott.