405(不允许的方法)Ionic 3 RestAPI

时间:2018-05-12 21:25:43

标签: angular api symfony ionic3 symfony-3.4

任何人都可以帮助我!

AuthProvider

registerUri: string = 'http://127.0.0.1:8000/users/add';
    signUp() {
        let user = {
          firstname: 'admin2',
          lastname: 'admin2',
          username: 'admin2',
          email: 'admin2@admin2.com',
          password: '0000',
          createdAt: new Date().toLocaleDateString()
        };
        return new Promise((resolve, reject) => {
          let headers = new Headers();
          headers.append('Content-Type', 'application/json');
          this.http.post(this.registerUri, JSON.stringify(user), { headers: headers })
            .subscribe(res => {
              resolve(res.json());
            }, (err) => {
              reject(err);
            });
        });
      }

RegisterPage

 register() {
    this.showLoader();
    this.authProvider.signUp().then((res) => {
      this.loading.dismiss();
      this.navCtrl.push(LoginPage);
    }).catch((err) => { this.presentToast('Error, Try Again!') });
}

控制器

 /**
     * @Route("/users/add" , name="register")
     * @Method("POST")
     */
    public function registerAction(Request $request,UserPasswordEncoderInterface $encoder)
    {

        $data=$request->getContent();

        $user=$this->get('jms_serializer')->deserialize($data,User::class,'json');

        $em=$this->getDoctrine()->getManager();
        $user->setPassword($encoder->encodePassword($user,$request->request->get('password')));
        $em->persist($user);
        $em->flush();

        $response=array(

            'code'=>12,
            'message'=>'User created!',
            'errors'=>null,
            'result'=>null
        );
        return new JsonResponse($response,Response::HTTP_CREATED);

    }

错误

  

选项http://127.0.0.1:8000/users/add 405(方法不允许)

     

无法加载http://127.0.0.1:8000/users/add:预检的响应包含无效的HTTP状态代码405。

     

Response screenshot

0 个答案:

没有答案