Yii + Backbone obtener datos

I need to receive data from Backbone in Yii Controller, but i do not know how to do that. I need to get it in my Controller and than save it to database

Backbone code

  var CallForm = Backbone.Model.extend({
            defaults: {
                projectname: null,
                sll: null,
                sspn: null,
                z: null,
                results: null,
                step: null
            },
            url: './index.php',
            validate: function(attr) {
                if( !(attr.projectname && attr.sll && attr.sll) > 9 ) {
                    return "Error Occurred";
                }
            },

        });

Ver

        var callView = Backbone.View.extend({
            el: '.callForm',
            events: {
                'click input.submit': 'getStatus'
            },
            getStatus: function(event){
                //for each inputs function value
                var NewProjectname = $('#projectname').val();
                var NewCall = $('#call').val();
                var NewSll = $('#sll').val();
                var NewSspn = $('#sspn').val();
                var NewZ = $('#z').val();
                var NewResults = $('#results').val();
                var NewStep = $('#step').val();
                //new model parent CallForm with value's from inputs
                var newrecord = new CallForm({
                    projectname: NewProjectname,
                    call: NewCall,
                    sll: NewSll,
                    sspn: NewSspn,
                    z: NewZ,
                    results: NewResults,
                    step: NewStep,
                    url: function() {
                        return '/callnew';
                    },
                });
                newrecord.save();
                var json = newrecord.toJSON();
                $('.test').html(JSON.stringify(json));
                return false;
            }
        });
        var callForm = new CallForm();
        var callView = new callView({
            model: callForm
        });

Yii Controller code index action

public function actionIndex()
    {
        if(isset($_POST['index.php']))
        {
          echo 'You';

        }
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('index');
    }

preguntado el 28 de mayo de 14 a las 13:05

2 Respuestas

If you are using Yii >= 1.1.3:

$json = Yii::app()->request->getRawBody();
$yourData = CJSON::decode($json);

http://www.yiiframework.com/doc/api/1.1/CHttpRequest#getRawBody-detail

contestado el 28 de mayo de 14 a las 20:05

Backbone Model send data in request payload. You can take it by this way:

$json = file_get_contents('php://input');
$data = CJSON::decode($json);

After that you can use this $data to update or create new record from php

contestado el 28 de mayo de 14 a las 17:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.