从离子调用laravel API时发生404错误

时间:2018-05-19 13:21:31

标签: laravel api ionic-framework

我试图从离子调用API,但它显示404错误 这是我的提供商代码

remoteservice.ts

  export class RemoteserviceProvider {
  public headers = new Headers( { 'X-API-KEY' : 
  'xxxxxxxxx' }); 
  public options = new RequestOptions({ headers: this.headers });
  constructor(public http: Http) {
  console.log('Hello RemoteserviceProvider Provider');
  }
   rec:any[]=[];
    use:any[]=[];
    login(credentials) {
    return new Promise((resolve, reject) => {
     this.http.post('http://localhost/my/v1/adminlogin', credentials, 
     {headers: this.headers})
    .subscribe(res => {
      resolve(res.json());
    }, (err) => {
      reject(err);
    });
   });
   } 

Login.ts

  doLogin() {
  this.showLoader();
  this.remoteService.login(this.loginData).then((result) => {
  this.loading.dismiss();
  this.responseData = result;
  console.log(this.responseData);
  if(this.responseData.message=='Login Success'){
  localStorage.setItem('loginData', JSON.stringify(this.responseData));
  if(this.responseData.user_type==1){
    if(this.responseData.project_type==null){
      this.presentToast('You are not assigned to any project');
    }
    else{
    if(this.responseData.project_type=='Concrete'){
      console.log(this.responseData.p_id)
      this.navCtrl.setRoot(ConcretePage, 
      {p_id:this.responseData.p_id, s_name:this.responseData.name,
       project:this.responseData.project,
       project_type:this.responseData.project_type,
       location:this.responseData.location});
        }
        else if(this.responseData.project_type=='Bricks'){
      this.navCtrl.setRoot(ProductionPage, 
       {p_id:this.responseData.p_id,s_name:this.responseData.name,
        project:this.responseData.project,
        project_type:this.responseData.project_type,
        location:this.responseData.location});
       }
       else{
        this.navCtrl.setRoot(DailyReportPage, 
      {p_id:this.responseData.p_id,s_name:this.responseData.name,
       project:this.responseData.project,
        project_type:this.responseData.project_type,
         location:this.responseData.location});
        }

        }

我的API代码是laravel

的index.php

  <?php

   //including the required files
   require_once '../include/DbOperation.php';
   require '.././libs/Slim/Slim.php';

   \Slim\Slim::registerAutoloader();

     $app = new \Slim\Slim();

     $app->hook('slim.before.dispatch', function () use ($app){

    $headers = request_headers();
    $response = array();
    $app = \Slim\Slim::getInstance();

    $api_key = $headers['X-API-KEY'];

    // this could be a MYSQL query that parses an API Key table, for example
    if($api_key == 'xxxxxxxxxxxxxxx') {
            $authorized = true;
    } else if ($api_key == NULL) {
            $response["error"] = true;
            $response["message"] = '{"error":{"text": "api key not sent" 
       }}';
            $app->response->headers['X-Authenticated'] = 'False';
            $authorized = false;
            $app->halt(401, $response['message']);
    } else {
            $response["error"] = true;
            $response["message"] = '{"error":{"text": "api key invalid" }}';
            $app->response->headers['X-Authenticated'] = 'False';
            $authorized = false;
    }

    if(!$authorized){ //key is false
            // dont return 403 if you request the home page
            $req = $_SERVER['REQUEST_URI'];
            if ($req != "/") {
            $app->halt('403', $response['message']); // or redirect, or 
  other something
            }
    }

         });

       $app->post('/adminlogin', function () use ($app) {

   $json = $app->request->getBody();
   $input = json_decode($json, true);  

$mobile= (int)$input['mobile'];
$password = (string)$input['password'];
$db = new DbOperation();
$response = array();
$response['report'] = array();
if ($db->adminLogin($mobile,$password)) {
    $admin = $db->getAdmin($mobile);
    $admin1 = $db->getassignedproject($mobile);
    $admin2 = $db->getprojecttype($admin1['p_id']);
    $admin4 = $db->updateadminlogin($mobile,$password);
    $response['error'] = false;
    $response['p_id']=$admin1['p_id'];
    $response['id'] = $admin['u_id'];
    $response['name'] = $admin['username'];
    $response['date'] = date('Y-m-d');
    $response['user_type'] = $admin['user_type'];
    $response['project'] = $admin1['p_name'];
    $response['project_type'] = $admin2['p_type'];
    $response['location'] = $admin2['location'];
    $response['message'] = "Login Success";

} else {
    $response['error'] = true;
    $response['message'] = "Invalid username or password";
}
echoResponse(200, $response);
});

在使用/ adminlogin调用API时,显示404错误 我不知道我哪里做错了。 任何人都可以给我一些想法来克服这一点。

先谢谢

0 个答案:

没有答案