2013-03-11 64 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

相關問題