PDA

View Full Version : Overcome the problem of the CORS (Access-Control-Allow-Origin) in MVC application



andvitiello
6 Oct 2012, 7:17 AM
I have built a web application using sencha touch 2. And therefore creating the whole infrastructure MVC. The client asks for the services to the server, that answers through json...in localhost everything is all right, but if I want to insert the server in a place and from the client to call the web application, the pages web is not released because the error Access-Control-Allow-Origin springs. How to overcome him/it, since my web application is a mvc?

here the source code that there is a request to client from server:




Ext.define('Twitter.store.Contacts', {
extend: 'Ext.data.Store',


config: {
model: 'Twitter.model.Contact',
autoLoad: true,

pageSize: 100,
clearOnPageLoad: false,

proxy: {
type: 'ajax',
url: 'http://server.render:9090/MVisualCRendServer/viewCommonClient.do'
},
reader: {
type: 'json',
successProperty: 'success',
root: 'tweet',
}
}
});




I cannot change this application through the proxy jsonp because I don't have a callback to send. The answer of the server is through a normal json.

jeanluca
6 Oct 2012, 10:06 AM
if JSONP is not an options, it cannot be done. You could of course write a backend yourself which is in the same domain as your client app. Let this backend make the call to the json-server which passes the response through to your client app! Or compile it to native!

andvitiello
7 Oct 2012, 7:18 AM
I have not understood a lot sincerely. But of certain I cannot create a something, that I know a file, from the client that it calls in turn a service on the server. Then with json and therefore with ajax it is not possible to resolve this problem of the cross origin? Possible this thing? to me it seems strange. If I wanted to use jsonp as should I do?

subimage
8 Oct 2012, 12:01 AM
If you're hosting on apache you can do this pretty simply with a rewrite rule.

Here's an example of mine in development.




<VirtualHost *:80>
ServerName touch.cashboard.i
ServerAlias touch.*
DocumentRoot "/Users/seth/Work/cb_mobile/app/trunk"
RewriteEngine On
RewriteRule ^/api_proxy/(.*)$ http://api.cashboard.i/$1 [P]
</VirtualHost>


So I just make all my requests prefixed with '/api_proxy/' and it redirects it to my actual API which is another host. [P] means PROXY, which assumes you of course have mod_proxy installed for Apache.

andvitiello
8 Oct 2012, 8:25 AM
for my part server I use Apache tomcat 7.0.26. This line of code that you have written me, where should I insert it? I don't succeed in finding any files on where to insert this code

andvitiello
8 Oct 2012, 8:59 AM
I have tried to use the proxy jsonp (also not inserting any function callback because I think that for my in demand services to the server it is not us of it need), but the error is visualized:

resource interpreted as script but transferred with mime type text / html

and any nestedlist is not visualized in the part client