weixin_33725515 2015-06-26 15:11 采纳率: 0%
浏览 35

Symfony和AJAX 404响应

I am calling a Symfony action via jQuery AJAX, however one of the actions is returning 404 and I am not sure why.

The Scenario.

An online system hosts a list of projects, these project can be added to a local system.

The user searches by Id and and if a project with that Id exists and they have not already added it the project will be added to their local system.

javascript

 //CHECK IF EXTERNAL PROJECT CAN BE IMPORTED
 $.get("/find-project/" + Id, function( data ) { << THIS ONE IS GOOD

 if(data['status'] === false){
 //PROJECT WITH THAT ID NOT FOUND
 }else{
 //PROJECT WITH THAT ID EXISTS


// HAVE YOU ALREADY ADDED THAT PROJECT
$.get("/check-project/" + Id, function(data){ << THIS RETURNS 404

if(data === false){
 //PROJECT HAS NOT BEEN ADDED.. ADD IT

 }else{
  //PROJECT HAS ALREADY BEEN ADDED
  }
  })
  }
   });

Symfony Controller PHP

/**
 * @Route("/find-project/{Id}", name="_findProject")
 * @Method({"GET"})
 */
public function findProjectByIdAction($Id)
{
    $project = $this->get('crm')->findProjectById($Id);
    $status = $project ? true : false;

    return new JsonResponse(array('status' => $status, 'project' => $project));
}



/**
 * @Route("/check-project/{Id}", name="_checkProject")
 * @Method({"GET"})
 */

//THIS FUNCTION CANNOT BE FOUND 404

public function checkIfProjectExistsAction($Id){

    $query = $this->getDoctrine()->getRepository('AppBundle:Project')->findOneBy(array('Id' => $Id));
    $cnt = count($query->getResult());

    $cnt == 0 ? $status = false : $status = true ;

    return new JsonResponse(array('status' => $status));
}

Already checked; Site is an available as route from route:debug in console

The requests return as

Remote Address:127.0.0.1:443 Request URL:https://localhost/check-project/12345 Request Method:GET Status Code:404 Not Found

Any help greatly appreciated

  • 写回答

1条回答 默认 最新

  • weixin_33734785 2015-06-27 10:12
    关注

    ->findOneBy(array('Id' => $Id));

    are you sure the 'Id' key is correct ?

    BTW. Returning true or false is ugly, you should throw not found exception if no records are present

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂