PDA

View Full Version : HTTP Headers support in Sencha Touch



irfaniqbal
28 Dec 2010, 8:10 AM
Hi,
I want to send an ajax request to my server with some HTTP Headers. In response to this request the server will send me Json or XML data. I want to ask is there any HTTP Header support in Sencha Touch. Thanks in advance.

AndreaCammarata
28 Dec 2010, 9:15 AM
Sure it supports headers:



Ext.Ajax.request({
url: '/Test.php',
method: 'POST',
headers: {
'MyHeader': 'This is my custom Header'
},
success: function(response, opts) {

}
});

Hope this helps

irfaniqbal
28 Dec 2010, 9:40 AM
thanks andrea for your quick reply. I have used headers in ScriptTagProxy to get Json data from Server. but browser throwing following error:
"Resource interpreted as script but transferred with MIME type text/html"
please reply me ASAP

AndreaCammarata
28 Dec 2010, 12:26 PM
You are welcome infariqbal.
Do you really need ScriptTagProxy? ScriptTagProxy is useful when you need to load data from a domain other than the one your application is running on. Is this your case?
If yes, which language are you using to develop your server side? (php, asp.net, ecc.)

irfaniqbal
29 Dec 2010, 2:21 AM
i need to call web services from another server and for that ScriptTagProxy is best solution. These services are developed in PHP.

Have you any idea about the error i have mentioned in my last post? please reply ASAP. Thanks anyways.

AndreaCammarata
29 Dec 2010, 6:29 AM
About your error I think you need to format your return data in a different way.
You need to take a look at the example posted inside the documentation on how your json data has to be formatted:



$callback = $_REQUEST['callback'];

// Create the output object.
$output = array('a' => 'Apple', 'b' => 'Banana');

//start output
if ($callback) {
header('Content-Type: text/javascript');
echo $callback . '(' . json_encode($output) . ');';
} else {
header('Content-Type: application/x-json');
echo json_encode($output);
}


Does your data is returned in the posted way?

irfaniqbal
29 Dec 2010, 6:56 AM
yes i m following exactly you have mentioned in your post. but i did this after my previous post of this thread. but now the problem is that when i take the console.log of my store. it only prints empty square brackets []. The returned Json is like that:


stcCallback1001({
"supplements": [
{
"id": "1484",
"plan_id": "1734",
"user_supplement_id": "398",
"supplement_id": "7",
"name": "Alapars\ufffd",
"instructions": "",
"unit": "Cup",
"dosage": "1",
"intake": "0",
"supplement_times": "4:1:0",
"plan_date": "2010-10-14",
"created": "2010-10-14 00:04:16",
"updated": "2010-10-14 00:04:16",
"created_by": "776",
"updated_by": "776",
"patient_added": 1
},
{
"id": "1482",
"plan_id": "1734",
"user_supplement_id": "396",
"supplement_id": "17",
"name": "Black Cohosh Plus\u00ae",
"instructions": "",
"unit": "Packet",
"dosage": "1",
"intake": "0",
"supplement_times": "2:1:0",
"plan_date": "2010-10-14",
"created": "2010-10-14 00:04:16",
"updated": "2010-10-14 00:04:16",
"created_by": "776",
"updated_by": "776",
"patient_added": 1
},
{
"id": "1486",
"plan_id": "1734",
"user_supplement_id": "408",
"supplement_id": null,
"name": "dfd",
"instructions": null,
"unit": "Tablet (s)",
"dosage": "12",
"intake": "0",
"supplement_times": "8:3:0~3:3:0~4:3:0~5:3:0",
"plan_date": "2010-10-14",
"created": "2010-10-14 00:04:16",
"updated": "2010-10-14 00:04:16",
"created_by": "776",
"updated_by": "776",
"patient_added": 0
}
]
})

i m using ScriptTagProxy to get Json from an external server. The question is whether i extract actual Json from callback function manually or scripttagproxy will handle it automatically. Please provide an example if possible. thanks

irfaniqbal
11 Jan 2011, 8:18 AM
Sure it supports headers:



Ext.Ajax.request({
url: '/Test.php',
method: 'POST',
headers: {
'MyHeader': 'This is my custom Header'
},
success: function(response, opts) {

}
});

Hope this helps

Hi,
I am using above mentioned code in my app. But when i send Headers, my server did not receive anything in $_SERVER[HTTP_AUTHORIZATION] Array. would you please help me to resolve this problem. Below is the code to extract XML from Server:


var oLogin = new Ext.data.Store( {
model: 'User',
autoLoad: true,
proxy : new Ext.data.ScriptTagProxy( { url: FLT.gv.serverUrl+'/Security/create_session'} ),
header: 'Authorization: TRUEREST username=babar&password=password'
} );

AndreaCammarata
11 Jan 2011, 11:25 AM
irfaniqbal I think you are not able to set headers for ScriptTagProxy.
I check again deeply in the Sencha docs and there's no "headers" config param for that kind of proxy.
It seems that you are allowed to set that param only for proxy with an "AjaxProxy" superclass.
Anyway, if you have to use another kind of proxy, change the header param with headers and use the same json sintax of my example:



...
headers: {
'yourHeaderName': 'Your header value'
},
...


Hope this helps

irfaniqbal
11 Jan 2011, 9:46 PM
thanks andrea for your reply. I also use headers in ScriptTagProxy but still unable to resolve the problem. So i also figure out that there is no support for headers in ScriptTagProxy. if you got anything new about headers in ScriptTagProxy please share your knowledge. Thanks once again.