【发布时间】:2018-05-09 11:42:20
【问题描述】:
帮助!我正在通过在线教程学习 php,最好的学习方法是做某事,我实际上是按程序方式做的,而且它有效,现在我在 OOP 和 MVC 中做同样的事情(我自己的 mvc 实现只是为了学习),在我必须使用 jquery/ajax 之前它也很顺利......现在我的问题是 JSON 中收到的响应。
这是在程序中相同的 Ajax 调用响应的屏幕截图:
以下是在 OOP/MVC 中不起作用的相同 Ajax 调用响应的屏幕截图:
包含 jQuery 的页面代码:
$("#full-input").on('change', function() {
var customer = $("#full-input").val();
$.ajax({
url: 'getSingle',
data: {id: customer},
type: 'post',
success: function (result) {
alert(result);//for debuging
}
});
});
控制器处理请求:
class CustomerController extends Controller{
public function index(){
$customer = new CustomerModel();
$data = $customer->index();
$this->setData($data);
$this->setTitle('Customer List');
$this->setView(VIEW_PATH.'customer/all_cust.php');
}
public function getSingle(){
$sintgle = new CustomerModel();
if(isset($_POST['id'])){
$result = $sintgle->singleCust($_POST['id']);
echo json_encode($result);
$this->setTitle('Single Customer');
}else{
$data = $sintgle->index();//gets all customers to populate drop-down select
$this->setData($data);
$this->setTitle('Select Customer');
$this->setView(VIEW_PATH.'customer/single_cust.php');
}
}
}
我在这里没有做什么?
【问题讨论】:
-
您检查过 AJAX 中的 URL 吗?通过查看您在控制器的方法中返回的数据,确保它到达正确的位置
-
我可以从图像中看到它正在撞击您的控制器,因为回显正在显示数据。也许您应该考虑设置内容类型 - stackoverflow.com/a/4064468/2401021
标签: php jquery model-view-controller