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!!”}
I had some problems with this as well. My solution:
$data = json_decode(file_get_contents('php://input'), true);
$this->site_model->create($data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With