gpt4 book ai didi

php - CakePHP 尝试发送 ajax 请求

转载 作者:行者123 更新时间:2023-12-01 07:15:11 26 4
gpt4 key购买 nike

我正在尝试向 Controller 发送 ajax 请求,它应该返回一些内容,但没有。我的元素(View/Elements/list.ctp):

<script type="text/javascript">
$(document).ready(function(){
$('#click').click(function(){
$.ajax({
type: "POST",
url: '<?php echo Router::url(array('controller' => 'products', 'action' => 'showProducts')); ?>',
success: function(data){
alert(data);
}
});
});
});
</script>
<p id="click">Click me!</p>

Controller 产品:

<?php
class ProductsController extends AppController {
public $helpers = array('Js' => array('Jquery'));
public $components = array('RequestHandler');
var $name = 'Products';

function showProducts(){

return 'This should to return in jQuery data';
}
}
?>

最佳答案

在 cakePHP 2.x 中,您的 Controller 需要以这种方式将 json 数据返回到 $.ajax 请求:

<?php
App::uses('AppController', 'Controller');
App::uses('JsBaseEngineHelper', 'View/Helper');

class AjaxtestsController extends AppController {

function beforeFilter() {
parent::beforeFilter();
}

public function returnsSomthing()
{
$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();
$jquerycallback = $_POST["callback"];
//do something and then put in $data those things you want to return.
//$data will be transformed to JSON or what you configure on the dataType.

echo JsBaseEngineHelper::object($data,array('prefix' => $jquerycallback.'({"totalResultsCount":'.count($data).',"ajt":','postfix' => '});'));
}

}

改进的答案

您可以使用 Controller 的方法在数据库上搜索数据:find(),但我将搜索查询移至模型:

首先:在模型代码处添加标记行

App::uses('AppModel', 'Model');
App::uses('Sanitize', 'Utility'); // <---
App::uses('JsBaseEngineHelper', 'View/Helper'); // <---

第二:创建一个执行搜索的模型方法:

public function getdata(){
$input = NULL;
$query = array();
$sql = NULL;
$data = array();
$i = 0;

$input = $_GET["name_startsWith"]; // <-- obtains the search parameter
$input = Sanitize::clean($input); // <-- prepares the search parameter

$sql = "select * from atable where condition like '".$input."%';";

$query = $this->query($sql); // <-- the model execute the search

if ($query){
$c = count($query);
for($i=0;$i<$c;$i++){ // <-- iterate over the returned data
$json['id'] = $query[$i][0]['id'];
$json['column1'] = $query[$i][0]['column1'];
$json['column2'] = $query[$i][0]['column2'];
$data[] = $json; // <-- the data it's stored on an multiarray, to be converted to JSON
}
}
$jquerycallback = $_GET["callback"];
echo JsBaseEngineHelper::object($data,array('prefix' => $jquerycallback.'({"totalResultsCount":'.count($query).',"search":','postfix' => '});')); //<-- the data it´s returned as JSON
}

然后:在 Controller 上创建一个方法来调用搜索

public function getdata()
{
$layout = 'ajax'; //<-- No LAYOUT VERY IMPORTANT!!!!!
$this->autoRender = false; // <-- NO RENDER THIS METHOD HAS NO VIEW VERY IMPORTANT!!!!!
$this->Cliente->getclient(); // <-- Get the data
}

您可以通过 ajax 调用此方法,如下所示:

$.ajax({
url: "/application/controller/getdata",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 10,
name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.search, function( item ) {
return {
value: item.id,
label: item.column1+" "+item.column2
}

}));

}
});

查看更多信息:

关于php - CakePHP 尝试发送 ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19911685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com