PHP MVC框架中ajax请求中的URL,不知道如何?
我在 PHP MVC 框架中遇到 AJAX 请求问题:我不知道如何使用 jQuery 等将 JSON 格式的数据从控制器调用到视图。
我浪费了很多时间在互联网上寻找任何有用的提示,并尝试自己解决这个问题,但没有成功。
我认为问题在于我需要在哪里写URL;我无法正确地写出它。
这是控制器的代码:
public function indexAction()
{
$dbh = new PDO('mysql:dbname=myframework;host=localhost', 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare('SELECT variety ,fruit_id FROM fruit limit 10');
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$data = $stmt->fetchAll();
echo json_encode($data);
}
这是视图代码:
<!DOCTYPE html>
<html><head><meta charset="utf-8" /> </head>
<h1>grid ajax</h1> <body> <table id='grid'></table>
<script type="text/javascript" src="<?php echo PROJECT_URL ?>/views/search/jquery.js"></script>
<script type="text/javascript">
$(function(){
$.getJSON( myURL - HERE IS PROBLEM ! , function(json){
for (var i=0;i<json.length;i++) {
$('#grid').append("<tr><td>" + json[i].fruit_id + "</td><td>" + json[i].variety + "</td></tr>")
}
});
});
</script>
</body>
</html>
这是路由:
getUrlFor($controller = 'index', $action = 'index') { $route = array('controller' => $controller, 'action' => $action); $routes = $this->getRoutes(); $url = array_search($route, $routes); if ($url === false) { $url = $controller . '/' . $action; } $url = PROJECT_URL . '/' . $url; return $url; } }
和 public function urlFor()
:
public function urlFor($controller = 'index', $action = 'index')
{
return Router::getInstance()->getUrlFor($controller, $action);
}
该函数在此框架中随处可用; URL 形式示例:http://localhost/myframework/ajax/index, (var $myURL = "urlFor('ajax', 'index'); ?>";
- 在 jQuery 中我以这种形式使用它),只是不在 $.getJSON
中。我尝试以多种形式编写此 URL,但没有一种有效。这是简单的自定义框架。
I have a problem with AJAX request in PHP MVC framework: I don't know how to call JSON formatted data from controller to the view using, for example, jQuery.
I wasted many hours searching for any useful tip on the internet and trying to resolve this problem by myself but with no success.
I think the problem is where I need to write the URL; I can't write it correctly.
This is the code for the controller:
public function indexAction()
{
$dbh = new PDO('mysql:dbname=myframework;host=localhost', 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare('SELECT variety ,fruit_id FROM fruit limit 10');
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$data = $stmt->fetchAll();
echo json_encode($data);
}
And this is the view code:
<!DOCTYPE html>
<html><head><meta charset="utf-8" /> </head>
<h1>grid ajax</h1> <body> <table id='grid'></table>
<script type="text/javascript" src="<?php echo PROJECT_URL ?>/views/search/jquery.js"></script>
<script type="text/javascript">
$(function(){
$.getJSON( myURL - HERE IS PROBLEM ! , function(json){
for (var i=0;i<json.length;i++) {
$('#grid').append("<tr><td>" + json[i].fruit_id + "</td><td>" + json[i].variety + "</td></tr>")
}
});
});
</script>
</body>
</html>
Here is routing:
getUrlFor($controller = 'index', $action = 'index') { $route = array('controller' => $controller, 'action' => $action); $routes = $this->getRoutes(); $url = array_search($route, $routes); if ($url === false) { $url = $controller . '/' . $action; } $url = PROJECT_URL . '/' . $url; return $url; } }
and public function urlFor()
:
public function urlFor($controller = 'index', $action = 'index')
{
return Router::getInstance()->getUrlFor($controller, $action);
}
This function works in this framework everywere;
URL form example: http://localhost/myframework/ajax/index,
(var $myURL = "<?php echo $this->urlFor('ajax', 'index'); ?>";
- in jQuery I use it in this form), only not in $.getJSON
. I tried to write this URL in many forms, none of them worked. It is simple custom framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您的路由是如何设置的,因此无法提供具体帮助。但我通常做的是(我使用 codeigniter)为所有 ajax 调用创建一个单独的控制器。控制器中的函数从模型获取数据,将该数据传递给视图,并将视图发送给 ajax 请求。
Codeigniter 有(imo)非常好的路由,您可以使用 url
http://sitename.com/index.php/controller_name/function
调用 ajax 控制器,这就是您传递 ajax 调用的内容
希望如此完全有帮助。
I'm not sure how your routing is setup so can't offer specific help. But what I usually do is (I use codeigniter) make a separate controller for all ajax calls. The functions in the controller gets the data from the model, passes that data to the view and servers the view to the ajax request.
Codeigniter has (imo) very nice routing, you call the ajax controller with the url
http://sitename.com/index.php/controller_name/function
and that's what you pass the the ajax call
Hope this helps at all.