2013-03-11 65 views
0

我正在使用sencha touch 2.1的移动web应用程序。目前我正在制作一个登录系统。对此,我正在使用用户名和密码从本地计算机向远程服务器发送ajax请求。但我不断收到此错误移动网络应用程序的访问控制 - 允许来源问题

XMLHttpRequest cannot load http://something.com/login.php?_dc=1362983327250. Origin http://dev.vclouds.com is not allowed by Access-Control-Allow-Origin. 

在上面的错误http://dev.vclouds.com是我localhost。我以这种方式在我的apache配置中设置它。

这里是我的Ajax请求

Ext.Ajax.request({ 
     url: 'http://something.com/beta/webservice/login.php', 
     method: 'post', 
     params: { 
      user_name: username, 
      user_password: password 
     }, 
     success: function (response) { 
      var loginResponse = Ext.JSON.decode(response.responseText); 
      console.log(loginResponse); 
      if(loginResponse.success === "true") { 
       me.sessionToken = loginResponse.sessionToken; 
       me.signInSuccess(); 
      } else { 
       me.signInFailure(loginResponse.message); 
      } 
     }, 
     failure: function (response) { 
      me.sessionToken = null; 
      me.signInFailure('Login failed'); 
     }, 
     callback: function (opts, success, response) { 
      console.log('callback'); 
      console.log(opts); 
      console.log(success); 
      console.log(response); 
     } 
    }); 

码我怎样才能解决这个问题?

UPDATE

我也试过JSONP如下

Ext.data.JsonP.request({ //<-- line 35 
     url: 'http://something.com/beta/webservice/login.php', 
     callbackKey: 'callback', 
     //method: 'post', 
     params: { 
      user_name: username, 
      user_password: password 
     }, 
     callback: function (opts, success, response) { 
      console.log('callback'); 
      console.log(opts); 
      console.log(success); 
      console.log(response); 
     } 
    }); 

但我收到此错误

Uncaught TypeError: Cannot call method 'request' of undefined    Login.js:35 
+0

该链接不回答我的问题。 – 2619 2013-03-11 07:01:50

+0

该链接*确实*回答这个问题。 – Quentin 2013-03-11 07:02:47

+0

它是否也适用于sencha touch应用程序? – 2619 2013-03-11 07:03:32

回答

0

您需要使用JsonP而不是阿贾克斯。只有当您处于同一个域时,才能使用Ajax,即请求正在创建,并且所请求的资源应位于同一个域中。在这种情况下,您有不同的域名,因此您需要用户JsonP请求。

他们的方式使用JSONP是,

Ext.data.JsonP.request({ 
     url: 'http://something.com/beta/webservice/login.php', 
     callbackKey: 'callback', 
     params: { 
      user_name: username, 
      user_password: password 
     }, 
     callback:function(result,response){ 
       console.log(response); 
     } 
    }); 

这里要考虑的重要一点是,与JsonP数据将只通过GET方法发送。您不能使用POST方法。服务器还需要返回请求中发送的callback以便继续执行callback函数。你可以在我的答案here上找到一些信息。

也为CORS即您需要添加所需的类Cross-Origin Resource Sharing相关信息请参考我的另一个答案here

编辑。像requires:['Ext.data.proxy.JsonP']在您的文件中。 Refere undefined error with JSONP

+1

使用JSON-P是Ajax ...并且有不少替代方案。 – Quentin 2013-03-11 06:53:49

+0

请参阅我的更新。我已经使用JsonP,但它给了我另一个错误。 – 2619 2013-03-11 07:00:58

+0

如果我们无法控制服务器配置,我想知道如何制作CORS。 – SachinGutte 2013-03-11 07:03:51

相关问题