【问题标题】:Symfony returns a 404 error when .getJSON is calledSymfony 在调用 .getJSON 时返回 404 错误
【发布时间】:2016-04-04 13:10:30
【问题描述】:

我最近将我的 Symfony 项目部署到了​​生产环境。 除了 .getJSON jquery 函数外,一切都在加载。 这将返回 404 错误。现在奇怪的是它可以在本地工作。

这是代码。

jQuery:

$.getJSON('/ClientThisYear', function(response) {

路由:

app:
   resource: "@AppBundle/Controller"
   type:     annotation

AppBundle/控制器:

/**
 * @Route("/ClientThisYear")
 */
public function ClientThisYear()
{
    $em = $this->getDoctrine()->getManager();
    $ClientStats = $em->getRepository('ClientBundle:Client')
        ->ClientStats();
    $ClientArray = array();
    foreach($ClientStats[0] as $Arr)
    {
        $ClientArray[] = $Arr;
    }
    $Return = json_encode($ClientArray);
    return new Response($Return,200,array('Content-Type'=>'application/json'));
}

所以它工作了一段时间,但过了一段时间它开始返回 404 错误。

我试过的是:

  • 清除缓存。
  • 从本地再次部署。

错误日志:

[2015-12-30 14:50:43] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /ClientThisYear" (from "http://example.nl/web/admin")" at /home/**/**/var/cache/prod/classes.php line 2377 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0): No route found for \"GET /ClientThisYear\" (from \"http://example.nl/web/admin\") at /home/**/**/var/cache/prod/classes.php:2377, Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0):  at /home/**/**/var/cache/prod/appProdUrlMatcher.php:255)"} []

有人可以帮我解决这个问题

【问题讨论】:

  • 你在开发模式下试过了吗?
  • 是的,我已经在 dev 模式和 prod 模式下在本地尝试过。当我这样做时,没有任何错误,并且正确显示了记录。问题仅在服务器上。它是本地副本的精确副本。
  • 您的本地路径前缀是否与生产中的匹配?例如http://localhost/myapp/ClientThisYearhttp://mywebsite/ClientThisYear。你可以通过浏览器调用路径吗?日志中关于错误的内容是什么?或者生产没有启用mod_rewrite
  • 它与同一控制器中的 /admin 路由具有相同的路径。这显示正确。所以我认为路径设置正确。我将使用错误日志编辑我的问题
  • 命令 - 'php app/console router:debug' 可能会显示提示

标签: php jquery symfony


【解决方案1】:

在 Javascript 中使用路由可能需要FOSJsRouting

此捆绑包允许您将路由与 javascript 一起使用

【讨论】:

    猜你喜欢
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 2011-08-11
    相关资源
    最近更新 更多