2016-08-03 97 views
0

我试图将数据从表单发布到我在网上主机上部署的Web服务器中的代码控制器。它适用于我的笔记本电脑,但在build.phonegap.com中构建并安装到我的手机中后,它无法工作。下面是index.html文件中的代码:Phonegap/AJAX/Codeigniter - Ajax发布不起作用

的Javascript:

function sendMessage(){ 
      //get values from the form 
      var msgFrom = $('#msgFrom').val(); 
      var msgTo = $('#msgTo').val(); 
      var msg = $('#msg').val(); 

      //works up to here only after building on build.phonegap.com 
      $.ajax({ 
       traditional: true, 
       type: 'POST', 
       dataType:'json', 
       url: '<...>/welcome/savePostData', 
       data: 'numberTo='+msgTo+'&numberFrom='+msgFrom+'&message='+msg, 

       success: function(resp) { 
        Materialize.toast("Posted", 5000); 

       } 

      }); 

     } 

下面是我的HTML代码中的资源:

<script type="text/javascript" src="js/jquery-3.1.0.min.js"></script> 
    <script type="text/javascript" src="js/jquery.mobile-1.4.5.js"></script> 
    <script type="text/javascript" src="js/jquery.mobile-1.4.5.min.js"></script> 

其他详情:

  • 我有在ubuntu上只使用这个命令创建应用程序:cordova create <appname>
  • 我仍然在运行android kitkat (lol
  • 我部署了我控制器freewebhostingarea.com

回答

0

请你的项目中添加白名单插件。

cordova plugin add cordova-plugin-whitelist --save 

这将启用您的Android设备中的http请求。并检查你的Android AndroidManifest.xml.It需要这个权限

<uses-permission android:name="android.permission.INTERNET" /> 

在最后你的config.xml

<access origin="*"/> 
<allow-intent href="*"/> 
<allow-navigation href="*"/> 

添加这些行那么你的HTTP请求将正常工作thanks.For进一步的细节请看这Whitelist

+0

嗨,感谢您的答案,我做了你说的一切,它仍然无法正常工作。哪个AndroidManifest.xml我编辑呢? 'cordovaLib'文件夹中的文件夹或'android'文件夹中的文件夹? –

0

它听起来像移动ajax没有启用。你有没有设置$ .support.cors为真?

来源:http://demos.jquerymobile.com/1.0/docs/pages/phonegap.html

+0

我在哪里启用它? –

+0

不太确定,说实话,你有jQuery的设置文件的地方?我对phonegap完全不熟悉,我只能通过Google搜索真正快速地找到答案,但它似乎可以解决您的问题 – jwerardi

0

为了使用跨域AJAX请求,你需要在你的config.xml文件的末尾添加以下代码。这是什么对我来说:

<access origin="*"/> 
    <gap:plugin name="cordova-plugin-whitelist" source="npm"/> 
    <access url="*" subdomains="true"/> 
    <allow-intent href="http://*/*"/>