【问题标题】:having issues with posting json to the database - 301 moved permanently symfony将 json 发布到数据库时遇到问题 - 301 永久移动 symfony
【发布时间】:2017-07-22 14:34:46
【问题描述】:

我正在 symfony 中创建一个 web 服务,并且我正在尝试对数据库进行 post 调用。在使用邮递员发布帖子时,我收到了永久移动的错误 301。 这是我的控制器代码

/**
 * @Route("/post/influ/")
 */
public function postAction(Request $request)
 {
   $data = new User;
   $firstname = $request->get('firstname');
   $lastname = $request->get('lastname');
   $username = $request->get('username');
   $gender = $request->get('gender');
   $email = $request->get('email');
   $country = $request->get('country');
   $state = $request->get('state');
   $password = $request->get('password');
   $retypePassword = $request->get('retypePassword');
 if(empty($firstname) || empty($lastname))
 {
   return new View("NULL VALUES ARE NOT ALLOWED", Response::HTTP_NOT_ACCEPTABLE); 
 } 
  $data->setFirstname($firstname);
  $data->setLastname($lastname);
  $data->setUsername($username);
  $data->setGender($gender);
  $data->setEmail($email);
  $data->setCountry($country);
  $data->setState($state);
  $data->setPassword($password);
  $data->setRetypePassword($retypePassword);
  $em = $this->getDoctrine()->getManager();
  $em->persist($data);
  $em->flush();
   return (" Added Successfully", Response::HTTP_OK);
 }

这是我使用邮递员发布到数据库但返回 301 作为响应永久移动的 json

{
"id": 3,
"firstname": "xyz",
"lastname": "xyz",
"username": "hello",
"gender": "Male",
"email": "hello@yahoo.com",
"country": "MyContry",
"state": "MyState",
"password": "password",
"retype_password": "password"
}

这是我发帖的网址

http://localhost:88/HelloApi/web/app_dev.php/post/influ

请问有什么问题。谢谢

【问题讨论】:

    标签: php json symfony redirect


    【解决方案1】:

    我没有代表发表评论,但如果此操作位于需要登录的文件结构中的某个位置,则 301 可能会将您的 Web 服务移动到登录页面,当然它不知道该做什么做。你知道你将 301 重定向到哪里吗?

    【讨论】:

      【解决方案2】:

      首先,您可以返回更清晰的响应:

      <?php
      return new Response(301);
      

      我认为重定向问题不是来自您的控制器,而是更多来自您对 security.yml 文件的配置。你能把访问控制部分复制到这里吗?

      【讨论】:

        猜你喜欢
        • 2016-06-08
        • 2013-09-26
        • 1970-01-01
        • 2019-11-04
        • 2012-07-10
        • 1970-01-01
        • 2019-10-14
        • 2018-06-18
        • 2018-06-23
        相关资源
        最近更新 更多