我正在努力学习使用PhoneGap,但仍在尝试将CodeIgniter完全作为后端添加。我已经成功地使用jQuery从我的CI控制器中加载了某些内容到我的PhoneGap Android应用程序中,但似乎无法正确地向服务器提交任何内容。您需要一个REST服务器才能通过PhoneGap与CI通信吗?我打算使用ajax post将信息发送到CI,但迄今为止无法使其正常工作。如果有人可以帮助我跨越这个障碍,我将不胜感激。谢谢
控制器:
<?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);
}
}
在PhoneGap设备上的Javascript
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"
});
}
add_article
类中:$this->output->enable_profiler(TRUE);
它将输出所有捕获的数据(POST 等)。也许您只是打错了某些东西,但是分析器是您的好朋友! - Jakub