PDA

View Full Version : How to consume external webservice in sencha touch2



Rameshlamani
11 May 2012, 12:44 AM
Hi. i am new to sencha touch2. i want to consume external web service. i have written below code it is always going to failure alert msg not working! in console giving like this error msg like XMLHttpRequest cannot load http://localhost/SLS.BRND.Services/Service1.asmx/Helloworld. Origin http://localhost:49692 is not allowed by Access-Control-Allow-Origin.
Blog.js:29
Object
Blog.js:30Response Status:- 0

My code is Here: Please help what is the problem

Ext.define("GS.view.Blog", {
extend: 'Ext.navigation.View',
xtype: 'blog',
config: {
title: 'WebService',
scrollable: true,
items: [
{
xtype: 'button',
text: 'Click Me',
height: 40,
width: 200,
listeners: {
tap: function () {
Ext.Ajax.request({
method: 'get',
url: 'http://localhost/SLS.BRND.Services/Service1.asmx/Helloworld',
method: 'POST',
params: {
format: 'JSON:'
},
success: function (response, request) {
alert('Working!')
console.log(response);
console.log('Response:-' + response.responseText)
},
failure: function (response, request) {
alert('Not Working!')
console.log(response);
console.log('Response Status:- ' + response.status)
}
});
}
}
}
]
}
});

oportocala
12 May 2012, 4:17 PM
It sounds like a cros domain communication issue.

You can look into Sencha's JSONP Proxy (http://docs.sencha.com/touch/2-0/#!/api/Ext.data.proxy.JsonP) if you can modify the webservice to support JSONP (http://en.wikipedia.org/wiki/JSONP) or CORS (http://www.html5rocks.com/en/tutorials/cors/) if you're feeling progressive.

Also very IMPORTANT: If you plan on packaging your client as a App using PhoneGap or whatever you need to know that cross domain limitations don't apply. Same happens when you open the index file directly in your browser, it will stop caring about cross-domain policy.

If you're using Chrome you can disable these via a flag in its config. I used this and it worked for me: http://opensourcehacker.com/2010/11/29/disabling-cross-domain-security-check-for-ajax-development-in-google-chrome/
(http://opensourcehacker.com/2010/11/29/disabling-cross-domain-security-check-for-ajax-development-in-google-chrome/)
Good luck