2013-03-28 50 views
5

嗨,我正在開發一個應用程序爲Android(使用phonegap,html5和JavaScript),它連接到遠程mysql數據庫。 我創建了一個RESTful(codeigniter)Web服務來訪問mysql數據庫,然後Android應用程序可以調用Web服務來獲取或發佈數據到數據庫,使用XML或JSON作爲數據格式。從android服務器獲取數據與jquery阿賈克斯phonegap

在使用jquery ajax的web瀏覽器中,我成功地獲得了結果,但在android應用程序中沒有得到響應。

jQuery.ajax({ 
url : 'http://10.10.1.129/index.php/apiauth/auth/?'+jQuery("#form-login").serialize(), 
async :true, 
cache :false, 
/*crossDomain : true,*/ 
dataType : 'jsonp', 
success:function(data){ 
    alert(data); 
    } 
}); 

服務器:

require APPPATH.'/libraries/REST_Controller.php'; 
    class Apiauth extends REST_Controller 
    { 
     function auth_get() 
     { 
      $this->load->model('mauth'); 
      $username = $this->input->get('username') ? $this->input->get('username') : $this->get('username'); 
      $password = $this->input->get('password') ? $this->input->get('password') : $this->get('password'); 
      $auth  = $this->mauth->getUserLogin('*',$username,$password); 
      $row  = $auth->row(); 
      if($row){ 
       $data = array('username'=>$row->username, 'fullname'=>$row->fullname,'error'=>FALSE); 
      }else{ 
       $data = array('error'=>true); 
      } 
      $this->response($data, 200); 
     } 
+0

我在這裏看不到任何Android代碼......? – Tushar 2013-03-28 07:18:05

+0

ups對不起,我使用phonegap,html5和javascript開發android應用程序 – Aditya 2013-03-28 07:22:20

回答

5

您是否添加與Ajax請求指定的IP地址的訪問來歷?該文件位於您的application-dir/res/xml/config.xml本地主機默認情況下是允許的,但您需要在使用時添加其他主機。所以,你的config.xml文件看起來應該是這樣

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
     Licensed to the Apache Software Foundation (ASF) under one 
     or more contributor license agreements. See the NOTICE file 
     distributed with this work for additional information 
     regarding copyright ownership. The ASF licenses this file 
     to you under the Apache License, Version 2.0 (the 
     "License"); you may not use this file except in compliance 
     with the License. You may obtain a copy of the License at 

     http://www.apache.org/licenses/LICENSE-2.0 

     Unless required by applicable law or agreed to in writing, 
     software distributed under the License is distributed on an 
     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
     KIND, either express or implied. See the License for the 
     specific language governing permissions and limitations 
     under the License. 
--> 
<cordova> 
    <!-- 
    access elements control the Android whitelist. 
    Domains are assumed blocked unless set otherwise 
    --> 

    <access origin="http://127.0.0.1*"/> <!-- allow local pages --> 
    <access origin="http://10.10.1.129*"/> <!-- allow requests from your server --> 


    <!-- <access origin="https://example.com" /> allow any secure requests to example.com --> 
    <!-- <access origin="https://example.com" subdomains="true" /> such as above, but including subdomains, such as www --> 
    <access origin=".*"/> 

    <log level="DEBUG"/> 
    <preference name="useBrowserHistory" value="false" /> 
<plugins> 
    <plugin name="App" value="org.apache.cordova.App"/> 
    <plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/> 
    <plugin name="Device" value="org.apache.cordova.Device"/> 
    <plugin name="Accelerometer" value="org.apache.cordova.AccelListener"/> 
    <plugin name="Compass" value="org.apache.cordova.CompassListener"/> 
    <plugin name="Media" value="org.apache.cordova.AudioHandler"/> 
    <plugin name="Camera" value="org.apache.cordova.CameraLauncher"/> 
    <plugin name="Contacts" value="org.apache.cordova.ContactManager"/> 
    <plugin name="File" value="org.apache.cordova.FileUtils"/> 
    <plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/> 
    <plugin name="Notification" value="org.apache.cordova.Notification"/> 
    <plugin name="Storage" value="org.apache.cordova.Storage"/> 
    <plugin name="Temperature" value="org.apache.cordova.TempListener"/> 
    <plugin name="FileTransfer" value="org.apache.cordova.FileTransfer"/> 
    <plugin name="Capture" value="org.apache.cordova.Capture"/> 
    <plugin name="Battery" value="org.apache.cordova.BatteryListener"/> 
    <plugin name="SplashScreen" value="org.apache.cordova.SplashScreen"/> 
</plugins> 
</cordova> 
+0

哇感謝它的工作... – Aditya 2013-03-28 08:46:21

+0

Phonegap似乎在2.3.0版本後纔開始關注此設置。我有josh指定的條目,但在域名中包含拼寫錯誤。在Cordova/Phonegap 2.3.0下一切正常。 當我升級到V2.7.0時,AJAX會調用成功函數,但響應總是爲空。當我糾正拼寫錯誤 - 賓果都再次工作。 我發現subdomains =「true」元素特別有用,因爲我們必須允許帶有或不帶有'www'的域名,正如註釋中所建議的那樣。 – 2013-05-08 07:04:49

0

<access origin=".*"/> didnt曾在,我們必須把我們Ajax請求期間調用特定的URL。

<access origin="http://10.10.1.129"/>這工作。