使用json和ajax cakephp 3从控制器返回数据值

时间:2015-04-29 01:34:36

标签: ajax json cakephp cakephp-3.0

我在表单中工作,每个表单都有很多输入,现在用于在我的数据库中插入此输入我在cakephp 3中使用ajax一切正常,只是一个问题,当我想要返回数据库中插入数据时这个数据以代码html返回,我无法访问信息示例 - >状态,消息,数据。 在我的控制器中有这个:

 public function insertPassenger()
 {
  $this->layout = 'ajax';
  $passenger = $this->Passengers->newEntity();
  $dato=$this->request->data();
  if ($this->request->is('ajax')) {
      $passenger = $this->Passengers->patchEntity($passenger, $this->request->data);
      $passenger->date = date('Y-m-d H:i:s');
      if ($this->Passengers->save($passenger)) {

        $data = [
         'content' => $dato,
         'status' => 'success',
         'message' => 'El pasajero fue registrado correctamente',
        ];

      } else {
        $data = [
         'content' => $dato,
         'status' => 'error',
         'message' => 'Verifique los datos',
        ];
      }
  }
  $this->set(compact('data')); // Pass $data to the view
  $this->set('_serialize', 'data'); // Let the JsonView class know what variable to use
}

现在我的js有这个

$(document).on('submit','form',function(e) {
    e.preventDefault();
    var values = $(this).serialize();
    var options = {};
    $.ajax({
       url: "/passengers/insertPassenger.json",
       type: "post",
       data: values,
       success: function(dato){
           console.log(dato);
       },
       error:function(e){
           console.log(e)
       }
    });
});

这里的一切都很好,但是当打印结果显示Google Chrome与console.log(dato);时,我已经完成了(最后我的信息很重要):

<pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5540307e9b1de-trace').style.display = (document.getElementById('cakeErr5540307e9b1de-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: array_combine(): Both parameters should have an equal number of elements [<b>CORE/src/ORM/Rule/ExistsIn.php</b>, line <b>102</b>]<div id="cakeErr5540307e9b1de-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5540307e9b1de-code').style.display = (document.getElementById('cakeErr5540307e9b1de-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5540307e9b1de-context').style.display = (document.getElementById('cakeErr5540307e9b1de-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5540307e9b1de-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$primary</span><span style="color: #007700">,</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$entity</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">extract</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_fields</span><span style="color: #007700">)</span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);</span></span></code></pre><pre id="cakeErr5540307e9b1de-context" class="cake-context" style="display: none;">$entity = object(App\Model\Entity\Passenger) {

    &#039;new&#039; =&gt; true,
    &#039;accessible&#039; =&gt; [
        &#039;travel_id&#039; =&gt; true,
        &#039;nombre&#039; =&gt; true,
        &#039;ci&#039; =&gt; true,
        &#039;edad&#039; =&gt; true,
        &#039;genero&#039; =&gt; true,
        &#039;num_emergencia&#039; =&gt; true,
        &#039;numero_asiento&#039; =&gt; true,
        &#039;orden_asiento&#039; =&gt; true,
        &#039;date&#039; =&gt; true,
        &#039;update&#039; =&gt; true,
        &#039;travel&#039; =&gt; true,
        &#039;invoices&#039; =&gt; true
    ],
    &#039;properties&#039; =&gt; [
        &#039;nombre&#039; =&gt; &#039;sasd&#039;,
        &#039;ci&#039; =&gt; (int) 23,
        &#039;edad&#039; =&gt; (int) 23,
        &#039;genero&#039; =&gt; &#039;MASCULINO&#039;,
        &#039;num_emergencia&#039; =&gt; (int) 21,
        &#039;numero_asiento&#039; =&gt; &#039;14&#039;,
        &#039;orden_asiento&#039; =&gt; &#039;4_2&#039;,
        &#039;travel_id&#039; =&gt; (int) 1,
        &#039;date&#039; =&gt; &#039;2015-04-28 21:14:38&#039;
    ],
    &#039;dirty&#039; =&gt; [
        &#039;nombre&#039; =&gt; true,
        &#039;ci&#039; =&gt; true,
        &#039;edad&#039; =&gt; true,
        &#039;genero&#039; =&gt; true,
        &#039;num_emergencia&#039; =&gt; true,
        &#039;numero_asiento&#039; =&gt; true,
        &#039;orden_asiento&#039; =&gt; true,
        &#039;travel_id&#039; =&gt; true,
        &#039;date&#039; =&gt; true
    ],
    &#039;original&#039; =&gt; [],
    &#039;virtual&#039; =&gt; [],
    &#039;errors&#039; =&gt; [],
    &#039;repository&#039; =&gt; &#039;Passengers&#039;

}
$options = [
    &#039;errorField&#039; =&gt; &#039;travel_id&#039;,
    &#039;message&#039; =&gt; &#039;This value does not exist&#039;,
    &#039;atomic&#039; =&gt; true,
    &#039;associated&#039; =&gt; true,
    &#039;checkRules&#039; =&gt; true,
    &#039;checkExisting&#039; =&gt; true,
    &#039;_primary&#039; =&gt; true,
    &#039;repository&#039; =&gt; object(App\Model\Table\PassengersTable) {

        &#039;registryAlias&#039; =&gt; &#039;Passengers&#039;,
        &#039;table&#039; =&gt; &#039;passengers&#039;,
        &#039;alias&#039; =&gt; &#039;Passengers&#039;,
        &#039;entityClass&#039; =&gt; &#039;App\Model\Entity\Passenger&#039;,
        &#039;associations&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;behaviors&#039; =&gt; [[maximum depth reached]],
        &#039;defaultConnection&#039; =&gt; &#039;default&#039;,
        &#039;connectionName&#039; =&gt; &#039;default&#039;

    }
]
$nulls = (int) 0
$schema = object(Cake\Database\Schema\Table) {
    [protected] _table =&gt; &#039;travels&#039;
    [protected] _columns =&gt; [
        &#039;id&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;fecha_hora&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;precio&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;date&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;update&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;output_id&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;destination_id&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;user_id&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;bus_id&#039; =&gt; [
            [maximum depth reached]
        ]
    ]
    [protected] _indexes =&gt; [
        &#039;fk_travels_outputs1_idx&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;fk_travels_destinations1_idx&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;fk_travels_users1_idx&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;fk_travels_buses1_idx&#039; =&gt; [
            [maximum depth reached]
        ]
    ]
    [protected] _constraints =&gt; [
        &#039;primary&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;fk_travels_buses1&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;fk_travels_destinations1&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;fk_travels_outputs1&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;fk_travels_users1&#039; =&gt; [
            [maximum depth reached]
        ]
    ]
    [protected] _options =&gt; [
        &#039;engine&#039; =&gt; &#039;InnoDB&#039;,
        &#039;collation&#039; =&gt; &#039;utf8_general_ci&#039;
    ]
    [protected] _temporary =&gt; false
    [protected] _columnKeys =&gt; [
        &#039;type&#039; =&gt; null,
        &#039;length&#039; =&gt; null,
        &#039;precision&#039; =&gt; null,
        &#039;null&#039; =&gt; null,
        &#039;default&#039; =&gt; null,
        &#039;comment&#039; =&gt; null
    ]
    [protected] _columnExtras =&gt; [
        &#039;string&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;integer&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;biginteger&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;decimal&#039; =&gt; [
            [maximum depth reached]
        ],
        &#039;float&#039; =&gt; [
            [maximum depth reached]
        ]
    ]
    [protected] _indexKeys =&gt; [
        &#039;type&#039; =&gt; null,
        &#039;columns&#039; =&gt; [[maximum depth reached]],
        &#039;length&#039; =&gt; [[maximum depth reached]],
        &#039;references&#039; =&gt; [[maximum depth reached]],
        &#039;update&#039; =&gt; &#039;restrict&#039;,
        &#039;delete&#039; =&gt; &#039;restrict&#039;
    ]
    [protected] _validIndexTypes =&gt; [
        (int) 0 =&gt; &#039;index&#039;,
        (int) 1 =&gt; &#039;fulltext&#039;
    ]
    [protected] _validConstraintTypes =&gt; [
        (int) 0 =&gt; &#039;primary&#039;,
        (int) 1 =&gt; &#039;unique&#039;,
        (int) 2 =&gt; &#039;foreign&#039;
    ]
    [protected] _validForeignKeyActions =&gt; [
        (int) 0 =&gt; &#039;cascade&#039;,
        (int) 1 =&gt; &#039;setNull&#039;,
        (int) 2 =&gt; &#039;setDefault&#039;,
        (int) 3 =&gt; &#039;noAction&#039;,
        (int) 4 =&gt; &#039;restrict&#039;
    ]
}
$field = &#039;travel_id&#039;
$alias = &#039;Travels&#039;
$primary = [
    (int) 0 =&gt; &#039;Travels.id&#039;,
    (int) 1 =&gt; &#039;Travels.output_id&#039;,
    (int) 2 =&gt; &#039;Travels.destination_id&#039;,
    (int) 3 =&gt; &#039;Travels.user_id&#039;,
    (int) 4 =&gt; &#039;Travels.bus_id&#039;
]</pre><pre class="stack-trace">array_combine - [internal], line ??
Cake\ORM\Rule\ExistsIn::__invoke() - CORE/src/ORM/Rule/ExistsIn.php, line 102
Cake\ORM\RulesChecker::Cake\ORM\{closure}() - CORE/src/ORM/RulesChecker.php, line 378
Cake\ORM\RulesChecker::Cake\ORM\{closure}() - CORE/src/ORM/RulesChecker.php, line 378
Cake\ORM\RulesChecker::checkCreate() - CORE/src/ORM/RulesChecker.php, line 258
Cake\ORM\RulesChecker::check() - CORE/src/ORM/RulesChecker.php, line 231
Cake\ORM\Table::checkRules() - CORE/src/ORM/Table.php, line 2148
Cake\ORM\Table::_processSave() - CORE/src/ORM/Table.php, line 1410
Cake\ORM\Table::Cake\ORM\{closure}() - CORE/src/ORM/Table.php, line 1367
Cake\Database\Connection::transactional() - CORE/src/Database/Connection.php, line 561
Cake\ORM\Table::save() - CORE/src/ORM/Table.php, line 1368
App\Controller\PassengersController::insertPassenger() - APP/Controller/PassengersController.php, line 170
Cake\Controller\Controller::invokeAction() - CORE/src/Controller/Controller.php, line 412
Cake\Routing\Dispatcher::_invoke() - CORE/src/Routing/Dispatcher.php, line 114
Cake\Routing\Dispatcher::dispatch() - CORE/src/Routing/Dispatcher.php, line 87
[main] - ROOT/webroot/index.php, line 37</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5540307e9c451-trace').style.display = (document.getElementById('cakeErr5540307e9c451-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (4096)</a>: Argument 1 passed to Cake\Database\Expression\QueryExpression::_addConditions() must be of the type array, boolean given, called in /home/widrogo/Code/estacion/vendor/cakephp/cakephp/src/Database/Expression/QueryExpression.php on line 123 and defined [<b>CORE/src/Database/Expression/QueryExpression.php</b>, line <b>478</b>]<div id="cakeErr5540307e9c451-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5540307e9c451-code').style.display = (document.getElementById('cakeErr5540307e9c451-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5540307e9c451-context').style.display = (document.getElementById('cakeErr5540307e9c451-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5540307e9c451-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">*&nbsp;@return&nbsp;</span><span style="color: #0000BB">void</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">*/</span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">protected&nbsp;function&nbsp;</span><span style="color: #0000BB">_addConditions</span><span style="color: #007700">(array&nbsp;</span><span style="color: #0000BB">$conditions</span><span style="color: #007700">,&nbsp;array&nbsp;</span><span style="color: #0000BB">$types</span><span style="color: #007700">)</span></span></code></pre><pre id="cakeErr5540307e9c451-context" class="cake-context" style="display: none;">$this = object(Cake\Database\Expression\QueryExpression) {
    [protected] _conjunction =&gt; &#039;AND&#039;
    [protected] _conditions =&gt; []
    [protected] _typeMap =&gt; object(Cake\Database\TypeMap) {}
}</pre><pre class="stack-trace">Cake\Database\Expression\QueryExpression::_addConditions() - CORE/src/Database/Expression/QueryExpression.php, line 478
Cake\Database\Expression\QueryExpression::add() - CORE/src/Database/Expression/QueryExpression.php, line 123
Cake\Database\Query::_conjugate() - CORE/src/Database/Query.php, line 1653
Cake\Database\Query::where() - CORE/src/Database/Query.php, line 761
Cake\ORM\Table::exists() - CORE/src/ORM/Table.php, line 1256
Cake\ORM\Association::__call() - CORE/src/ORM/Association.php, line 837
Cake\ORM\Association\BelongsTo::exists() - CORE/src/ORM/Rule/ExistsIn.php, line 103
Cake\ORM\Rule\ExistsIn::__invoke() - CORE/src/ORM/Rule/ExistsIn.php, line 103
Cake\ORM\RulesChecker::Cake\ORM\{closure}() - CORE/src/ORM/RulesChecker.php, line 378
Cake\ORM\RulesChecker::Cake\ORM\{closure}() - CORE/src/ORM/RulesChecker.php, line 378
Cake\ORM\RulesChecker::checkCreate() - CORE/src/ORM/RulesChecker.php, line 258
Cake\ORM\RulesChecker::check() - CORE/src/ORM/RulesChecker.php, line 231
Cake\ORM\Table::checkRules() - CORE/src/ORM/Table.php, line 2148
Cake\ORM\Table::_processSave() - CORE/src/ORM/Table.php, line 1410
Cake\ORM\Table::Cake\ORM\{closure}() - CORE/src/ORM/Table.php, line 1367
Cake\Database\Connection::transactional() - CORE/src/Database/Connection.php, line 561</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5540307e9d172-trace').style.display = (document.getElementById('cakeErr5540307e9d172-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Invalid argument supplied for foreach() [<b>CORE/src/Database/Expression/QueryExpression.php</b>, line <b>484</b>]<div id="cakeErr5540307e9d172-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5540307e9d172-code').style.display = (document.getElementById('cakeErr5540307e9d172-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5540307e9d172-context').style.display = (document.getElementById('cakeErr5540307e9d172-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5540307e9d172-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$typeMap&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">typeMap</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">types</span><span style="color: #007700">(</span><span style="color: #0000BB">$types</span><span style="color: #007700">);</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"></span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$conditions&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$k&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">)&nbsp;{</span></span></code></pre><pre id="cakeErr5540307e9d172-context" class="cake-context" style="display: none;">$this = object(Cake\Database\Expression\QueryExpression) {
    [protected] _conjunction =&gt; &#039;AND&#039;
    [protected] _conditions =&gt; []
    [protected] _typeMap =&gt; object(Cake\Database\TypeMap) {}
}
$conditions = false
$types = []
$operators = [
    (int) 0 =&gt; &#039;and&#039;,
    (int) 1 =&gt; &#039;or&#039;,
    (int) 2 =&gt; &#039;xor&#039;
]
$typeMap = object(Cake\Database\TypeMap) {
    [protected] _defaults =&gt; [
        &#039;Travels.id&#039; =&gt; &#039;integer&#039;,
        &#039;id&#039; =&gt; &#039;integer&#039;,
        &#039;Travels.fecha_hora&#039; =&gt; &#039;datetime&#039;,
        &#039;fecha_hora&#039; =&gt; &#039;datetime&#039;,
        &#039;Travels.precio&#039; =&gt; &#039;float&#039;,
        &#039;precio&#039; =&gt; &#039;float&#039;,
        &#039;Travels.date&#039; =&gt; &#039;datetime&#039;,
        &#039;date&#039; =&gt; &#039;datetime&#039;,
        &#039;Travels.update&#039; =&gt; &#039;datetime&#039;,
        &#039;update&#039; =&gt; &#039;datetime&#039;,
        &#039;Travels.output_id&#039; =&gt; &#039;integer&#039;,
        &#039;output_id&#039; =&gt; &#039;integer&#039;,
        &#039;Travels.destination_id&#039; =&gt; &#039;integer&#039;,
        &#039;destination_id&#039; =&gt; &#039;integer&#039;,
        &#039;Travels.user_id&#039; =&gt; &#039;integer&#039;,
        &#039;user_id&#039; =&gt; &#039;integer&#039;,
        &#039;Travels.bus_id&#039; =&gt; &#039;integer&#039;,
        &#039;bus_id&#039; =&gt; &#039;integer&#039;
    ]
    [protected] _types =&gt; []
}</pre><pre class="stack-trace">Cake\Database\Expression\QueryExpression::_addConditions() - CORE/src/Database/Expression/QueryExpression.php, line 484
Cake\Database\Expression\QueryExpression::add() - CORE/src/Database/Expression/QueryExpression.php, line 123
Cake\Database\Query::_conjugate() - CORE/src/Database/Query.php, line 1653
Cake\Database\Query::where() - CORE/src/Database/Query.php, line 761
Cake\ORM\Table::exists() - CORE/src/ORM/Table.php, line 1256
Cake\ORM\Association::__call() - CORE/src/ORM/Association.php, line 837
Cake\ORM\Association\BelongsTo::exists() - CORE/src/ORM/Rule/ExistsIn.php, line 103
Cake\ORM\Rule\ExistsIn::__invoke() - CORE/src/ORM/Rule/ExistsIn.php, line 103
Cake\ORM\RulesChecker::Cake\ORM\{closure}() - CORE/src/ORM/RulesChecker.php, line 378
Cake\ORM\RulesChecker::Cake\ORM\{closure}() - CORE/src/ORM/RulesChecker.php, line 378
Cake\ORM\RulesChecker::checkCreate() - CORE/src/ORM/RulesChecker.php, line 258
Cake\ORM\RulesChecker::check() - CORE/src/ORM/RulesChecker.php, line 231
Cake\ORM\Table::checkRules() - CORE/src/ORM/Table.php, line 2148
Cake\ORM\Table::_processSave() - CORE/src/ORM/Table.php, line 1410
Cake\ORM\Table::Cake\ORM\{closure}() - CORE/src/ORM/Table.php, line 1367
Cake\Database\Connection::transactional() - CORE/src/Database/Connection.php, line 561</pre></div></pre>{
    "content": {
        "nombre": "sasd",
        "ci": "23",
        "edad": "23",
        "genero": "MASCULINO",
        "num_emergencia": "21",
        "numero_asiento": "14",
        "orden_asiento": "4_2",
        "travel_id": "1"
    },
    "status": "success",
    "message": "El pasajero fue registrado correctamente"
}

0 个答案:

没有答案