Welcome Guest, Not a member yet? Register   Sign In
codeigniter 3 rest api with angular 5 post method raise error 405 method not allowed
#1

(This post was last modified: 10-24-2018, 11:20 PM by rupamhazra.)

codeigniter 3 rest api with angular 5 post method raise error 405 method not allowed. Please suggest
Reply
#2

That's hard to say without knowing your code/setup.

When in doubt, test one thing at a time, start with your API with postman (https://www.getpostman.com/) and see if you can replicate the issue.
Reply
#3
Wink 
(This post was last modified: 10-25-2018, 12:11 AM by rupamhazra.)

(10-24-2018, 11:52 PM)ragingTorch Wrote: That's hard to say without knowing your code/setup.

When in doubt, test one thing at a time, start with your API with postman (https://www.getpostman.com/) and see if you can replicate the issue.

 Hi,

 it's working from postman but it's not working from angular post request.I added my rest, other php page.

controller code
<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

class Api extends REST_Controller {
function __construct(){

parent::__construct();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');

header('Content-Type: application/json');




$this->load->model('Apimodel','apimodel');
$this->load->model('Adminmodel','adminmodel');
$this->load->model('Usermodel','usermodel');
$this->load->model('Blogmodel','blogmodel');
$this->load->model('Companymodel','companymodel');

$this->load->library('pagination');
//$this->load->model('Settingsmodel','settingsmodel');

}
public function userLogin_post(){

$data = [];
$data['email'] = $this->post('email');
$data['password'] = $this->post('password');
$result = $this->usermodel->userLogin($data);
//pre($result,1);
if ($result['status'])
       {
        $this->setResponse(REST_Controller::HTTP_CREATED,TRUE, $result['message'],'create'); // OK (200) being the HTTP response code
       }
       else
       {
        $this->setResponse(REST_Controller::HTTP_INTERNAL_SERVER_ERROR,$result['status'],$result['message']);
       }
}



and this code in routes page 

$route['api/userlogin'] = 'api/userLogin'; 


I am trying to login using credentials by post method from angular 5

Attached Files Thumbnail(s)
   

.php   config.php (Size: 18.63 KB / Downloads: 118)
.php   REST_Controller.php (Size: 80.93 KB / Downloads: 119)
.php   rest.php (Size: 20.88 KB / Downloads: 124)
Reply
#4

I did project with angularJS and CI3 Restful api about 2 years ago.

There was a prefix that you need to add to json response:

PHP Code:
   public function random_cars_get()
 
    {
         
$this->_angularjs_json_prefix =  ")]}',\n";
 
        $filter = array();
 
        $limit 10;
 
        $cars $this->car_search_model->get_best_price_cars($filter$limit);
 
        $return['success'] = true;
 
        $return['cars'] = $cars;

 
        echo $this->_angularjs_json_prefix json_encode($return);

 
    

I don't know is that is case in Angular 5
Reply
#5

Also you controller method name structure should be as follows:

If doing POST request
1. your URL is controllername/user
2. your controller method name is public function user_post(){}

If doing GET request
1. your URL is controllername/user
2. your controller method name is public function user_get(){}

If doing DELETE request
1. your URL is controllername/user
2. your controller method name is public function user_delete(){}

and so on. These informations should be in documentation of CI rest api
Reply
#6

(This post was last modified: 10-25-2018, 12:14 AM by rupamhazra.)

(10-25-2018, 12:09 AM)neuron Wrote: Also you controller method name structure should be as follows:

If doing POST request
1. your URL is controllername/user
2. your controller method name is public function user_post(){}

If doing GET request
1. your URL is controllername/user
2. your controller method name is public function user_get(){}

If doing DELETE request
1. your URL is controllername/user
2. your controller method name is public function user_delete(){}

and so on. These informations should be in documentation of CI rest api

I already done this and i have posted the code in 2nd thread.Please check and i also added my issue screenshot.
Reply
#7

(This post was last modified: 10-25-2018, 01:47 AM by neuron.)

In your screenshot I see that your request method is OPTIONS.
as your method name is userLogin_post your request method should be POST request.

Also first try without setting routing.
I mean rename your method userLogin_post to login_post
and do POST request to your-domain/api/login
Reply




Theme © iAndrew 2016 - Forum software by © MyBB