【问题标题】:Jquery AJAX Post: 500 (Internal Server Error)? in Symfony 3Jquery AJAX 帖子:500(内部服务器错误)?在 Symfony 3 中
【发布时间】:2017-07-28 09:27:29
【问题描述】:

我正在尝试将 id 发布到 Controller 操作,但我收到此错误: (我使用的是 Symfony 3)。

jquery-1.11.0.min.js:4 POST http://localhost/plate/web/app_dev.php/province_ajax_call?province_id=2 500(内部服务器错误)

这是我的 ajax 代码:

 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
{{ form(form_account) }}


<script>
$(document).ready(function(){
    $('#appbundle_account_Province').change(function(){
        var val = $('#appbundle_account_Province').val();
        $.ajax({
            type: "POST",
            url: "{{ url('province_ajax_call') }}?province_id=" + val,
            success: function(data){
                $('#appbundle_account_City').html('');
                $.each(data, function(key, value){
                    $('#appbundle_account_City').append('<option value="'+ key +'">'+ value +'</option>');
               })
               console.log('succes');
            }
        })
    })
})

这是我的控制器代码:

public function provinceAjaxCallAction(Request $request)
{

    if (!$request->isXmlHttpRequest())
    {
        throw new NotFoundHttpException();
    }

    $id = $request->query->get('province_id');
    var_dump($id);

    $cities = $this
              ->getDoctrine()
              ->getManager()
              ->getRepository('AppBundle:City')
              ->findByProvinceId($id);

    $result = array();
    foreach($cities as $city){
        $result[$city->getId()] = $city->getName();
    }
    return new JsonResponse($result);
}

public function indexAction(Request $request)
{
    $account = new Account();

    $form = $this->createForm(AccountType::class, $account);
    $form->handleRequest($request);

    if($form->isValid())
    {
        $this->getDoctrine()->getManager()->persist($account);
        $this->getDoctrine()->getManager()->flush();

        return $this->redirectToRoute('test_registration_homepage');
    }

    return $this->render('default/index.html.twig', array('form_account' => $form->createView()));


}
}

【问题讨论】:

  • 它可以是任何东西,从配置文件中的错字开始。您可以查看日志 - 它们会准确告诉您请求出了什么问题。
  • 我检查了日志,但我什么都不懂
  • @Sabra,您检查了哪些日志?也许您检查了错误的日志?如果您正在检查您的 PHP 日志,但不理解它,请在此处发布错误消息,以便我们让您知道它的含义。如果您不学习如何阅读错误日志消息,您将不会在您正在从事的任何项目中走得太远。

标签: php html ajax symfony-3.3


【解决方案1】:

我通过添加解决了我的问题

use Symfony\Component\HttpFoundation\JsonResponse;

在控制器中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2019-12-10
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多