Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing HTTP Request from Backbone.js to CodeIgniter to DB

I am trying to sync up model in Backbone.js to Codeigniter using a RESTful api written by Philip Sturgeon @ http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/ I am trying to add an entry to the user resource group. The problem is: I will get a new entry, but title would be 0 instead of "Hello World!!!" I believe the breakdown is at 'title'=>$this->post('title') in the controller, because when I replaced it with 'title'=>"FOO", Foo will show up in the DB. Any thoughts? Btw, should I put as the URL in backbone.js

url: "MyApp/index.php/app/user"    or
url: "MyApp/index.php/app/user/id/(xxx)"

Backbone.js

$(document).ready(function(){
var Item = Backbone.Model.extend({

defaults: {
  title: "Hello World!!!"
},

url: "MyApp/index.php/app/user"

});
var item=new Item;  
item.save();

app.php (Controller)

function user_post()
{
    $data=array(
    'id'=> NULL,
    'title'=>$this->post('title')
    );

    $result = $this->App_Model->create($data);
}

app_model.php (Model)

function create($data)
{

    $query = $this->db->insert('data', $data)
}

UPDATE::

This is from the Chrome Inspector

Request Method:POST

Status Code:200 OK

Request Headersview source

Accept:application/json, text/javascript, /; q=0.01

Connection:keep-alive

Content-Length:15

Content-Type:application/json

X-Requested-With:XMLHttpRequest

Request Payload {“title”:“my Content!!”}

like image 847
William Sham Avatar asked Feb 24 '23 08:02

William Sham


1 Answers

I had some problems with this as well. My solution:

$data = json_decode(file_get_contents('php://input'), true);
$this->site_model->create($data);
like image 153
Markus Jönsson Avatar answered Mar 06 '23 08:03

Markus Jönsson