【问题标题】:Updating page with jquery without refreshing the in php mvc使用 jquery 更新页面而不刷新 in php mvc
【发布时间】: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


【解决方案1】:

尝试在 $result 之后添加一个 var_dump,

var_dump($result);

并在 echo 语句后返回 false/true。

【讨论】:

  • 我的问题是:从响应截图中,json数据将页面源的其余部分附加到它...
  • 是的,这就是为什么我要求放置 var_dump 以检查 $result 变量中实际得到的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
  • 2020-07-28
相关资源
最近更新 更多