4

So I am getting better at using phonegap but am still trying to fully add codeigniter as the backend. I have been able to .load in jquery something from a controller of my CI to my phonegap android app but can't seem to submit anything TO the server properly. Do you need a rest server to communicate with CI from phonegap? I was planning on using ajax post to info to CI but so far unable to get it to work. I'd really appreciate it someone can help me over this hurdle. Thanks

link to relative answer

Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {


    public function index()
    {
        //$this->load->view('welcome_message');
    $data['query']=$this->site_model->get_last_ten_articles();
$this->load->view('partial1',$data);

    }

    public function addarticle(){
    $headline=$this->input->post('headline');
    $article=$this->input->post('article');
    $this->site_model->insert_entry($headline,$article);    

    }
}

Javascript(on phonegap device)

function add_article(){



$.ajax({
  type: 'POST',
  url: 'http://testlab.site40.net/droiddev/welcome/addarticle/',
  data: {"headline": "test headline","article": "test article"} 
  error: function(){alert('fail');},
  success: function(data){
   alert('article added');
  },
  dataType: "json"
});

}
Community
  • 1
  • 1
CI_Guy
  • 1,039
  • 2
  • 22
  • 39
  • Show the code you've written that doesn't successfully communicate with the codeigniter backend. Is this in the emulator or on a device? – Femi Sep 24 '11 at 13:53
  • Add the profiler to your `add_article` class: `$this->output->enable_profiler(TRUE);` It will output all data captured (POST, etc;) It could be you just typo'd something, but the profiler is your friend! – Jakub Sep 25 '11 at 02:58
  • @Jakub thanks for the profiler tip! totally forgot. it is useful :) – CI_Guy Sep 25 '11 at 05:39
  • @CI_Guy i'm trying to do the same, how did you integrated CI with phonegap ? its pretty late question but still could you please help ? And couldn't you do "base_url" in your ajax call ? – Habib Rehman Nov 29 '16 at 18:38

1 Answers1

6

First of all, lets get your example running, your post data is json, and the datatype is json but your CI implementation is accessing post variables.

The quick and dirty fix is to submit a uri string in the post data such as:

&headline=test%20headline&article=test%20article

This can be generated from a form with the jquery serialize function:

var myData = $('#form-id').serialize();

This post data will be set in the $_POST var on submission and then later accessible through the CI post function:

$this->input->post()

*Note: remeber to remove the dataType setting in the ajax call for this to work.

For a more politically correct way to solve this problem, you're going to want to leave your javascript alone (thats all good) but you need to set the CI backend up as a RESTful service, the default installed controller and input classes won't handle it. You need to use something like Phil Sturgeon's REST implementation:

  • There is a github project for the code,
  • A blog post (read this first - its a good short primer on REST servers for CI concerned usage),
  • And the tutorial you already know about.
  • Oh and a video on setting it up.
lsl
  • 4,371
  • 3
  • 39
  • 54