View Full Version : Sencha Touch JSON Writer Issue
cassebn
6 Dec 2010, 5:38 PM
With the following store I notice the writer isn't sending over a POST param of data but instead sending it in a "Request Payload". I haven't ever seen this before and was curious on how to handle it in my PHP file. With EXTJS I can just look for a POST param of "data" and iterate through the object.
How do you send a "data" object in a POST param with Sencha Touch but still utilize the stores writer functionality?
Here is what shows up in my Resource pane of developer tools in Safari:
Request Payload: {"data":[{"id":1,"username":"cassebn","fname":"foo","lname":"bar","password":"pwd","last_login":"2010-12-05 13:35:00"}]}
Ext.regStore("UserStore", {
model: 'UserModel',
sorters: 'lname',
getGroupString: function(record){
return record.get('username')[0];
},
proxy: {
type: 'ajax',
url: 'users.php',
reader: {
type: 'json',
root: 'data'
},
writer: {
type: 'json',
root: 'data'
}
},
autoLoad: true
});
mrsunshine
7 Dec 2010, 12:53 AM
Try the rest proxy instead http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.RestProxy
the rest proxy sends a POST request
jochem kuun
7 Mar 2011, 7:03 AM
I've got the same problem. I've found a temporary solution by using the following code to read the submitted data. I don't really know exactly why it works this way, but here's the code:
$raw = '';
$httpContent = fopen('php://input', 'r');
while ($kb = fread($httpContent, 1024)) {
$raw .= $kb;
}
fclose($httpContent);
$params = json_decode(stripslashes($raw));
print_r($params); //should contains the passed data decoded as array.
I'm using the following (test)model:
/*global Ext */
Ext.regModel('LessonReview', {
idProperty: 'id',
fields: [{
name: 'id',
type: 'int'
}, {
name: 'score',
type: 'int'
}, {
name: 'date_added',
type: 'date'
}, {
name: 'date_updated',
type: 'date'
}],
proxy: {
type: 'ajax',
url : '/poc/api/lessonreviews.php'
reader: {
type: 'json'
}
}
});
When a user presses a save button, i'd like to save the model to the database and perform some other functions like changing the view:
{
text: 'Opslaan',
ui: 'confirm',
scope: this,
handler: function(){
var data = this.getValues(),
//store = Ext.getStore('LessonReviews');
var review = Ext.ModelMgr.create(data, 'LessonReview');
review.save();
//store.sync(); dont know if this is needed???
Ext.getCmp('lesson-reviews').setActiveItem(0);
}
}
I'm using the Sencha MVC framework so the code inside the handler would normaly be placed inside an controller action.
When using the code above, a print_r($_POST) on the page '/poc/api/lessonreviews.php' results in an empty array. As the TS already stated the data IS available inside Request Payload.
I've tried changing the proxy type to 'rest' but this has no effect.
Are there other (easier) ways to get the data?
mikeycgto
14 Apr 2011, 6:45 AM
has anyone figured out how to make the POST body be URL encoded?
Bunchofstring
22 Sep 2011, 5:53 PM
I am having this issue as well with Sencha Touch 1.1. Can someone please help? This is my second try with Sencha Touch after I gave up in late-2010 because of issues with server requests (much like this). Not everyone can just re-purpose their backend to handle JSON....
Bunchofstring
22 Sep 2011, 7:03 PM
Okay... this may be ugly and goofy, but it works for me. The code below allows my server to understand the web app's command to create a new user on the backend. The response isn't understood yet, but the user account is created successfully. I guess the code could go into a store too, but that's for *you* to test.
In Sencha Touch 1.1, it urlencodes the form data/request payload (default is JSON encoded). For those of us with less than flexible backends (i.e. those that don't understand JSON or XML), it might be helpful.
Ext.regModel('Player', {
fields: ['name','mail'],
proxy: {
type: 'rest',
writer:{
type:'json',
writeRecords:function(request,data){
Ext.apply(data[0],request.params);
request.jsonData=Ext.urlEncode(data[0]);
return request;
},
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
url : 'my_service_endpoint_url',
extraParams:{
apikey:'sent_with_every_request',
},
}
});
var player = Ext.ModelMgr.create({name: 'Test', mail: 'test@test.com'}, 'Player');
player.save();
Please feel free to improve the code, but remember to share with the community ;o)
Powered by vBulletin® Version 4.1.5 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.