2013-03-28 59 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"/>这工作。