我正在尝试使用下拉菜单中所选客户的客户数据自动填写发票表单上的默认值。在下面照片中的下拉列表中选择客户后,我希望它自动填写下表中下一张照片中客户表中的数据地址。
到目前为止,这是我的发票add.ctp脚本,用于尝试获取数据。目前使用此脚本,我可以获取选择在sql查询中使用的客户ID,还可以在表单输入中设置数据
<script>
document.getElementById('customers').addEventListener('change',function(){
alert(this.value);
$('#customers').click(function(){
$.ajax({
type: "POST",
url: '<?php echo Router::url(array('controller' => 'Customers', 'action' => 'fill')); ?>',
success: function(data){
alert(data);
}
});
});
document.getElementById('name').value = "test"
document.getElementById('invoice_to_address').value = "test"
});
</script>
这是我在CustomersController中的填充功能。这是我出错的地方我认为我的查询可能是完全错误的,并且它没有搜索正确的东西并且错误地返回它,甚至可能甚至没有使用表单中的客户ID。目前它只返回没有数据但也没有错误,并且由于视图中的脚本而出现空警告框。
public function fill()
{
$layout = 'ajax'; // you need to have a no html page, only the data.
$this->autoRender = false; // no need to render the page, just plain data.
$data = array();
$id = $this->request->data();
$query = $this->Customers->find()
->where([
'id' => $id]);
$this->set(array(
'id' => $query,
'_serialize' => 'id'
));
}
更新
我的完整发票add.ctp
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<?php echo $this->Html->script('jquery.chained'); ?>
<script type="text/javascript">
$(document).ready(function () {
$("#contacts").chained("#customers");
});
</script>
<?php use Cake\Routing\Router; ?>
<?php
/**
* @var \App\View\AppView $this
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Invoices'), ['action' => 'index']) ?></li>
<li><?= $this->Html->link(__('List Customers'), ['controller' => 'Customers', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Customer'), ['controller' => 'Customers', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Customer Contacts'), ['controller' => 'CustomerContacts', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Customer Contact'), ['controller' => 'CustomerContacts', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Aircraft Registrations'), ['controller' => 'AircraftRegistrations', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Aircraft Registration'), ['controller' => 'AircraftRegistrations', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Shipping Companies'), ['controller' => 'ShippingCompanies', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Shipping Company'), ['controller' => 'ShippingCompanies', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Job Types'), ['controller' => 'JobTypes', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Job Type'), ['controller' => 'JobTypes', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Currencies'), ['controller' => 'Currencies', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Currency'), ['controller' => 'Currencies', 'action' => 'add']) ?></li>
</ul>
</nav>
<div class="invoices form large-9 medium-8 columns content">
<?= $this->Form->create($invoice) ?>
<fieldset>
<legend><?= __('Add Invoice') ?></legend>
<?php
echo $this->Form->input('start_date', ['empty' => false]);
echo $this->Form->input('close_date', ['empty' => true]);
echo $this->Form->input('customer_id', ['options' => $customers, 'empty' => true,'id'=>'customers']);
echo $this->Form->input('name', ['type' => 'text', 'id'=>'name']);
echo $this->Form->input('invoice_to_address', ['type' => 'text', 'id'=>'invoice_to_address']);
echo $this->Form->input('ship_to_address');
echo $this->Form->input('customer_contact_id', ['options' => $customerContacts, 'empty' => true,'id'=>'contacts']);
echo $this->Form->input('currency_id', ['options' => $currencies, 'default'=> 1, 'id'=>'currencies']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
<?= $this->Form->create(Null, ['type' => 'POST']) ?>
<?= $this->Form->end() ?>
</div>
<script>
jQuery('#name').autocomplete({source:'<?php echo Router::url(array('controller' => 'Customers', 'action' => 'search')); ?>'
});
</script>
<script>
document.getElementById('customers').addEventListener('change',function(){
var id = this.value;
alert(id);
var csrfToken = $('[name=_csrfToken]').val();
$.ajax({
type: "POST",
url: '<?php echo Router::url(array("controller" => "Customers", "action" => "fill")); ?>',
data: {'id' : id},
beforeSend: function(xhr){
xhr.setRequestHeader('X-CSRF-Token', csrfToken);
},
success: function(data){
alert(data);
data = JSON.parse(data);
alert("id: " + data.id);
}
});
document.getElementById('name').value = "test"
document.getElementById('invoice_to_address').value = "test"
document.getElementById('currencies').value = 3;
});
</script>
和控制器填充功能
public function fill(){
$layout = 'ajax'; // you need to have a no html page, only the data.
$this->autoRender = false; // no need to render the page, just plain data.
if ($this->request->is('ajax')) {
$id = $this->request->data['id'];
$query = $this->Customers->find()
->where([
'id' => $id
])->first();
echo json_encode($query);
}
}
当($ this-&gt; request-&gt;是('ajax'))存在时会发生什么。没有回应或预览。
当if($ this-&gt; request-&gt;是('ajax'))被删除时。
以下是完整的错误消息
{"id":0,"name":"Sky Works","country_id":1,"city_id":6,"address":"Sky works address in the customers table","postal_address":"Sky works shippng address in the customers table","phone":"","email":"","payment_terms_id":1,"stop_credit":false,"gst_percentage":null,"currency_id":"2","account_closed":false,"invoice_email":"","customer_notes":""}<pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8332d-trace').style.display = (document.getElementById('cakeErr5a1140bb8332d-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (512)</a>: Unable to emit headers. Headers sent in file=/home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php line=179 [<b>CORE/src/Http/ResponseEmitter.php</b>, line <b>48</b>]<div id="cakeErr5a1140bb8332d-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8332d-code').style.display = (document.getElementById('cakeErr5a1140bb8332d-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8332d-context').style.display = (document.getElementById('cakeErr5a1140bb8332d-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5a1140bb8332d-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> $message </span><span style="color: #007700">= </span><span style="color: #DD0000">"Unable to emit headers. Headers sent in file=</span><span style="color: #0000BB">$file</span><span style="color: #DD0000"> line=</span><span style="color: #0000BB">$line</span><span style="color: #DD0000">"</span><span style="color: #007700">;</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">if (</span><span style="color: #0000BB">Configure</span><span style="color: #007700">::</span><span style="color: #0000BB">read</span><span style="color: #007700">(</span><span style="color: #DD0000">'debug'</span><span style="color: #007700">)) {</span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB"> trigger_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">);</span></span></code></pre><pre id="cakeErr5a1140bb8332d-context" class="cake-context" style="display: none;">$response = object(Zend\Diactoros\Response) {
[protected] headers => [
'Content-Type' => [
[maximum depth reached]
]
]
[protected] headerNames => [
'content-type' => 'Content-Type'
]
[private] phrases => [
(int) 100 => 'Continue',
(int) 101 => 'Switching Protocols',
(int) 102 => 'Processing',
(int) 200 => 'OK',
(int) 201 => 'Created',
(int) 202 => 'Accepted',
(int) 203 => 'Non-Authoritative Information',
(int) 204 => 'No Content',
(int) 205 => 'Reset Content',
(int) 206 => 'Partial Content',
(int) 207 => 'Multi-status',
(int) 208 => 'Already Reported',
(int) 226 => 'IM used',
(int) 300 => 'Multiple Choices',
(int) 301 => 'Moved Permanently',
(int) 302 => 'Found',
(int) 303 => 'See Other',
(int) 304 => 'Not Modified',
(int) 305 => 'Use Proxy',
(int) 306 => 'Switch Proxy',
(int) 307 => 'Temporary Redirect',
(int) 308 => 'Permanent Redirect',
(int) 400 => 'Bad Request',
(int) 401 => 'Unauthorized',
(int) 402 => 'Payment Required',
(int) 403 => 'Forbidden',
(int) 404 => 'Not Found',
(int) 405 => 'Method Not Allowed',
(int) 406 => 'Not Acceptable',
(int) 407 => 'Proxy Authentication Required',
(int) 408 => 'Request Time-out',
(int) 409 => 'Conflict',
(int) 410 => 'Gone',
(int) 411 => 'Length Required',
(int) 412 => 'Precondition Failed',
(int) 413 => 'Request Entity Too Large',
(int) 414 => 'Request-URI Too Large',
(int) 415 => 'Unsupported Media Type',
(int) 416 => 'Requested range not satisfiable',
(int) 417 => 'Expectation Failed',
(int) 418 => 'I'm a teapot',
(int) 421 => 'Misdirected Request',
(int) 422 => 'Unprocessable Entity',
(int) 423 => 'Locked',
(int) 424 => 'Failed Dependency',
(int) 425 => 'Unordered Collection',
(int) 426 => 'Upgrade Required',
(int) 428 => 'Precondition Required',
(int) 429 => 'Too Many Requests',
(int) 431 => 'Request Header Fields Too Large',
(int) 444 => 'Connection Closed Without Response',
(int) 451 => 'Unavailable For Legal Reasons',
(int) 499 => 'Client Closed Request',
(int) 500 => 'Internal Server Error',
(int) 501 => 'Not Implemented',
(int) 502 => 'Bad Gateway',
(int) 503 => 'Service Unavailable',
(int) 504 => 'Gateway Time-out',
(int) 505 => 'HTTP Version not supported',
(int) 506 => 'Variant Also Negotiates',
(int) 507 => 'Insufficient Storage',
(int) 508 => 'Loop Detected',
(int) 510 => 'Not Extended',
(int) 511 => 'Network Authentication Required',
(int) 599 => 'Network Connect Timeout Error'
]
[private] reasonPhrase => ''
[private] statusCode => (int) 200
[private] protocol => '1.1'
[private] stream => object(Zend\Diactoros\Stream) {}
}
$maxBufferLength = (int) 8192
$file = '/home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php'
$line = (int) 179
$message = 'Unable to emit headers. Headers sent in file=/home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php line=179'</pre><pre class="stack-trace">Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 48
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 116
[main] - ROOT/webroot/index.php, line 37</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8392c-trace').style.display = (document.getElementById('cakeErr5a1140bb8392c-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Cannot modify header information - headers already sent by (output started at /home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php:179) [<b>CORE/src/Http/ResponseEmitter.php</b>, line <b>146</b>]<div id="cakeErr5a1140bb8392c-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8392c-code').style.display = (document.getElementById('cakeErr5a1140bb8392c-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8392c-context').style.display = (document.getElementById('cakeErr5a1140bb8392c-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5a1140bb8392c-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> $response</span><span style="color: #007700">-></span><span style="color: #0000BB">getStatusCode</span><span style="color: #007700">(),</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">(</span><span style="color: #0000BB">$reasonPhrase </span><span style="color: #007700">? </span><span style="color: #DD0000">' ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$reasonPhrase </span><span style="color: #007700">: </span><span style="color: #DD0000">''</span><span style="color: #007700">)</span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">));</span></span></code></pre><pre id="cakeErr5a1140bb8392c-context" class="cake-context" style="display: none;">$response = object(Zend\Diactoros\Response) {
[protected] headers => [
'Content-Type' => [
[maximum depth reached]
]
]
[protected] headerNames => [
'content-type' => 'Content-Type'
]
[private] phrases => [
(int) 100 => 'Continue',
(int) 101 => 'Switching Protocols',
(int) 102 => 'Processing',
(int) 200 => 'OK',
(int) 201 => 'Created',
(int) 202 => 'Accepted',
(int) 203 => 'Non-Authoritative Information',
(int) 204 => 'No Content',
(int) 205 => 'Reset Content',
(int) 206 => 'Partial Content',
(int) 207 => 'Multi-status',
(int) 208 => 'Already Reported',
(int) 226 => 'IM used',
(int) 300 => 'Multiple Choices',
(int) 301 => 'Moved Permanently',
(int) 302 => 'Found',
(int) 303 => 'See Other',
(int) 304 => 'Not Modified',
(int) 305 => 'Use Proxy',
(int) 306 => 'Switch Proxy',
(int) 307 => 'Temporary Redirect',
(int) 308 => 'Permanent Redirect',
(int) 400 => 'Bad Request',
(int) 401 => 'Unauthorized',
(int) 402 => 'Payment Required',
(int) 403 => 'Forbidden',
(int) 404 => 'Not Found',
(int) 405 => 'Method Not Allowed',
(int) 406 => 'Not Acceptable',
(int) 407 => 'Proxy Authentication Required',
(int) 408 => 'Request Time-out',
(int) 409 => 'Conflict',
(int) 410 => 'Gone',
(int) 411 => 'Length Required',
(int) 412 => 'Precondition Failed',
(int) 413 => 'Request Entity Too Large',
(int) 414 => 'Request-URI Too Large',
(int) 415 => 'Unsupported Media Type',
(int) 416 => 'Requested range not satisfiable',
(int) 417 => 'Expectation Failed',
(int) 418 => 'I'm a teapot',
(int) 421 => 'Misdirected Request',
(int) 422 => 'Unprocessable Entity',
(int) 423 => 'Locked',
(int) 424 => 'Failed Dependency',
(int) 425 => 'Unordered Collection',
(int) 426 => 'Upgrade Required',
(int) 428 => 'Precondition Required',
(int) 429 => 'Too Many Requests',
(int) 431 => 'Request Header Fields Too Large',
(int) 444 => 'Connection Closed Without Response',
(int) 451 => 'Unavailable For Legal Reasons',
(int) 499 => 'Client Closed Request',
(int) 500 => 'Internal Server Error',
(int) 501 => 'Not Implemented',
(int) 502 => 'Bad Gateway',
(int) 503 => 'Service Unavailable',
(int) 504 => 'Gateway Time-out',
(int) 505 => 'HTTP Version not supported',
(int) 506 => 'Variant Also Negotiates',
(int) 507 => 'Insufficient Storage',
(int) 508 => 'Loop Detected',
(int) 510 => 'Not Extended',
(int) 511 => 'Network Authentication Required',
(int) 599 => 'Network Connect Timeout Error'
]
[private] reasonPhrase => 'OK'
[private] statusCode => (int) 200
[private] protocol => '1.1'
[private] stream => object(Zend\Diactoros\Stream) {}
}
$reasonPhrase = 'OK'</pre><pre class="stack-trace">header - [internal], line ??
Cake\Http\ResponseEmitter::emitStatusLine() - CORE/src/Http/ResponseEmitter.php, line 146
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 54
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 116
[main] - ROOT/webroot/index.php, line 37</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb83ee9-trace').style.display = (document.getElementById('cakeErr5a1140bb83ee9-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Cannot modify header information - headers already sent by (output started at /home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php:179) [<b>CORE/src/Http/ResponseEmitter.php</b>, line <b>173</b>]<div id="cakeErr5a1140bb83ee9-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb83ee9-code').style.display = (document.getElementById('cakeErr5a1140bb83ee9-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb83ee9-context').style.display = (document.getElementById('cakeErr5a1140bb83ee9-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5a1140bb83ee9-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> $name</span><span style="color: #007700">,</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"> $value</span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">), </span><span style="color: #0000BB">$first</span><span style="color: #007700">);</span></span></code></pre><pre id="cakeErr5a1140bb83ee9-context" class="cake-context" style="display: none;">$response = object(Zend\Diactoros\Response) {
[protected] headers => [
'Content-Type' => [
[maximum depth reached]
]
]
[protected] headerNames => [
'content-type' => 'Content-Type'
]
[private] phrases => [
(int) 100 => 'Continue',
(int) 101 => 'Switching Protocols',
(int) 102 => 'Processing',
(int) 200 => 'OK',
(int) 201 => 'Created',
(int) 202 => 'Accepted',
(int) 203 => 'Non-Authoritative Information',
(int) 204 => 'No Content',
(int) 205 => 'Reset Content',
(int) 206 => 'Partial Content',
(int) 207 => 'Multi-status',
(int) 208 => 'Already Reported',
(int) 226 => 'IM used',
(int) 300 => 'Multiple Choices',
(int) 301 => 'Moved Permanently',
(int) 302 => 'Found',
(int) 303 => 'See Other',
(int) 304 => 'Not Modified',
(int) 305 => 'Use Proxy',
(int) 306 => 'Switch Proxy',
(int) 307 => 'Temporary Redirect',
(int) 308 => 'Permanent Redirect',
(int) 400 => 'Bad Request',
(int) 401 => 'Unauthorized',
(int) 402 => 'Payment Required',
(int) 403 => 'Forbidden',
(int) 404 => 'Not Found',
(int) 405 => 'Method Not Allowed',
(int) 406 => 'Not Acceptable',
(int) 407 => 'Proxy Authentication Required',
(int) 408 => 'Request Time-out',
(int) 409 => 'Conflict',
(int) 410 => 'Gone',
(int) 411 => 'Length Required',
(int) 412 => 'Precondition Failed',
(int) 413 => 'Request Entity Too Large',
(int) 414 => 'Request-URI Too Large',
(int) 415 => 'Unsupported Media Type',
(int) 416 => 'Requested range not satisfiable',
(int) 417 => 'Expectation Failed',
(int) 418 => 'I'm a teapot',
(int) 421 => 'Misdirected Request',
(int) 422 => 'Unprocessable Entity',
(int) 423 => 'Locked',
(int) 424 => 'Failed Dependency',
(int) 425 => 'Unordered Collection',
(int) 426 => 'Upgrade Required',
(int) 428 => 'Precondition Required',
(int) 429 => 'Too Many Requests',
(int) 431 => 'Request Header Fields Too Large',
(int) 444 => 'Connection Closed Without Response',
(int) 451 => 'Unavailable For Legal Reasons',
(int) 499 => 'Client Closed Request',
(int) 500 => 'Internal Server Error',
(int) 501 => 'Not Implemented',
(int) 502 => 'Bad Gateway',
(int) 503 => 'Service Unavailable',
(int) 504 => 'Gateway Time-out',
(int) 505 => 'HTTP Version not supported',
(int) 506 => 'Variant Also Negotiates',
(int) 507 => 'Insufficient Storage',
(int) 508 => 'Loop Detected',
(int) 510 => 'Not Extended',
(int) 511 => 'Network Authentication Required',
(int) 599 => 'Network Connect Timeout Error'
]
[private] reasonPhrase => 'OK'
[private] statusCode => (int) 200
[private] protocol => '1.1'
[private] stream => object(Zend\Diactoros\Stream) {}
}
$name = 'Content-Type'
$values = [
(int) 0 => 'text/html; charset=UTF-8'
]
$first = true
$value = 'text/html; charset=UTF-8'</pre><pre class="stack-trace">header - [internal], line ??
Cake\Http\ResponseEmitter::emitHeaders() - CORE/src/Http/ResponseEmitter.php, line 173
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 55
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 116
[main] - ROOT/webroot/index.php, line 37</pre></div></pre>
答案 0 :(得分:0)
更改:
$query = $this->Customers->find()->where(['id' => $id]);
致:
$query = $this->Customers->find()->where(['id' => $id])->first();
也使用&#34; echo&#34;在控制器中将数据返回给ajax。
echo json_encode($query);
然后应该返回json编码的数据。
修改强>
确保在ctp文件中创建了一个表单(获取csrf标记):
<?= $this->Form->create(Null, ['type' => 'POST']) ?>
<?= $this->Form->end() ?>
并将这些行添加到javascript代码中:
beforeSend: function(xhr){
xhr.setRequestHeader('X-CSRF-Token', csrfToken);
},
并添加&#34;数据&#34;到javascript,如:
var id = 1; //Your ID. I don't know what ID it is, so I just use 1.
var csrfToken = $('[name=_csrfToken]').val(); //Sorry, I forgot this.
$.ajax({
type: "POST",
url: '<?php echo Router::url(array("controller" => "Customers", "action" => "fill")); ?>',
data: {'id' : id},
beforeSend: function(xhr){
xhr.setRequestHeader('X-CSRF-Token', csrfToken);
},
success: function(data){
alert(data);
data = JSON.parse(data);
alert("id: " + data.id);
}
});
更改控制器:
public function fill(){
$layout = 'ajax'; // you need to have a no html page, only the data.
$this->autoRender = false; // no need to render the page, just plain data.
if ($this->request->is('ajax')) {
$id = $this->request->data['id'];
$query = $this->Customers->find()
->where([
'id' => $id
])->first();
echo json_encode($query);
}
}
修改强>
也许你没有使用CsrfToken。在这种情况下,您不需要加载CsrfToken。
像这样更改javascript代码:
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
//jQuery CDN. If you have loaded jQuery somewhere else, you don't need this.
</script>
<script>
var id = 1; //Your ID. I don't know what ID it is, so I just use 1.
$.ajax({
type: "POST",
url: '<?= $this->Url->build(array("controller" => "Customers", "action" => "fill")); ?>',
data: {'id' : id},
success: function(data){
alert(data);
data = JSON.parse(data);
alert("id: " + data.id);
}
});
</script>
使用此javascript,我认为您不必更改控制器(您不需要删除(&#39; ajax&#39;)。)
修改强>
也许如你所说,从控制器返回null。但我想检查错误消息是什么。请通过以下方式检查。
如果成功,则获得json字符串。您可以在收到后将其更改为javascript的对象。
如果失败,您可以从Google Chrome浏览器进行检查。它应该显示你得到的错误:
或者,也许cakephp可能会在html中返回错误消息。在这种情况下,你会收到这个:
这是原始的html代码,因此可能很难阅读。
请告诉我您收到的错误消息..
答案 1 :(得分:0)
这是Lechien。
似乎错误消息是:无法发出标头。在文件= / home / southpac / southpacificavionics.com / team / src / Controller / CustomersController.php第179行发送的标题。
从错误消息中,我怀疑BOM是否已添加到任何PHP文件中。请在此处查看问题:How to fix "Headers already sent" error in PHP
有时,Windows默认文本编辑器会自动将BOM添加到文件中。
您可以检查客户的控制器文件和模板(ctp文件)中是否有BOM?如果发现添加了BOM,请删除BOM。然后我提供的代码应该可以工作。