简体   繁体   English

将 HTTP 请求从 Backbone.js 传递到 CodeIgniter 到 DB

[英]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. 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试图向用户资源组添加一个条目。 The problem is: I will get a new entry, but title would be 0 instead of "Hello World,,."问题是:我将获得一个新条目,但标题将是 0 而不是“Hello World,,”。 I believe the breakdown is at 'title'=>$this->post('title') in the controller?我相信故障在 controller 中的 'title'=>$this->post('title') 处? because when I replaced it with 'title'=>"FOO", Foo will show up in the DB.因为当我用 'title'=>"FOO" 替换它时,Foo 会出现在数据库中。 Any thoughts?有什么想法吗? Btw, should I put as the URL in backbone.js顺便说一句,我应该把 URL 放在 backbone.js

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

Backbone.js 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) app.php(控制器)

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

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

app_model.php (Model) app_model.php(型号)

function create($data)
{

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

UPDATE::更新::

This is from the Chrome Inspector这是来自 Chrome Inspector

Request Method:POST请求方法:POST

Status Code:200 OK状态码:200 OK

Request Headersview source请求标头view source

Accept:application/json, text/javascript, / ;接受:应用程序/json,文本/javascript, / q=0.01 q=0.01

Connection:keep-alive连接:保持活动

Content-Length:15内容长度:15

Content-Type:application/json内容类型:应用程序/json

X-Requested-With:XMLHttpRequest X-请求-与:XMLHttpRequest

Request Payload {“title”:“my Content!!”}请求有效载荷 {“title”:“我的内容!!”}

I had some problems with this as well.我对此也有一些问题。 My solution:我的解决方案:

$data = json_decode(file_get_contents('php://input'), true);
$this->site_model->create($data);

You need to change this line under your Controller:您需要在 Controller 下更改此行:

$this->post('title')

To

$this->input->post('title')

or actually you can use plain PHP for accessing your POST global variables, like $_POST['title'].或者实际上你可以使用普通的 PHP 来访问你的 POST 全局变量,比如 $_POST['title']。

As reference, check the CodeIgniter's Input Class作为参考,检查CodeIgniter 的输入 Class

I'm not too sure as to why your title param is not getting passed.我不太确定为什么您的标题参数没有通过。 I would recommend using Firebug or Chrome Inspector to check if the title param actually got sent.我建议使用 Firebug 或 Chrome Inspector 来检查标题参数是否真的被发送了。 You can narrow down your debugging from there.您可以从那里缩小调试范围。

As for the URL, the RESTful way to do it would be to use MyApp/index.php/app/user/id" to manage a single user至于 URL,RESTful 方法是使用 MyApp/index.php/app/user/id" 来管理单个用户

EDIT编辑

It looks like your request is sent fine.看起来您的请求已发送正常。 I would examine the post data.我会检查帖子数据。 Why not print the post data and see what is received.为什么不打印发布数据,看看收到了什么。 My guess is PHP is not reading the JSON data correctly.我的猜测是 PHP 没有正确读取 JSON 数据。 You can use $decoded = json_decode($this->post('title')) to turn it into a php object.您可以使用 $decoded = json_decode($this->post('title')) 将其转换为 php object。

What about using $this->input->post('title');使用$this->input->post('title');怎么样? ? ? ( Reference ) 参考

Try accessing尝试访问

$this->request->body

It should contain the contents of all the values you are posting via Backbone.js它应该包含您通过 Backbone.js 发布的所有值的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM