【问题标题】:Not able to fetch data in codeigniter from angular 4 view无法从角度 4 视图中获取 codeigniter 中的数据
【发布时间】:2018-08-15 03:30:01
【问题描述】:

我在前端使用 Angular 4,在后端使用 CodeIgniter。在 angular 端,有一个表单,其中包含一些我需要在 CodeIgniter 端获取的输入值和图像,以便可以将它们保存在数据库中。但是对于图像,我希望将其保存在文件夹中,然后将其路径保存在数据库中

表格代码

<form ngNativeValidate [formGroup]="form" (ngSubmit)="onSubmitadd()" class="form-horizontal">
    <input type="text" class="form-control"  name="title" id="title" formControlName="title" minlength="10" maxlength="150" required>

    <input type="file" id="avatar" (change)="onFileChange($event)" #fileInput required>

    <button type="submit"  class="demo-loading-btn btn red savebtn">Save</button>
</form>



 onSubmitadd() {
    const formModel = this.form.value;
    this.loading = true;
     this.http.post('http://www.localhost/litehires/company/profile',formModel ,{
      })
        .subscribe(
          res => {
            console.log(res);
            $("#add-data .close").click();
            this.listBanner();
        },
          err => {
            console.log("Error occured");
          }
        );
  }

控制器代码

public function profile()   
      {  
        $res = $this->input->post('formModel');
        return $res;
      }

在控制台中检查时,数据正在 formModel 中传递,但我无法在控制器中获取数据。谁能告诉我如何获取数据和图像,以便我可以进一步处理它

【问题讨论】:

  • 在控制器中将return $res; 替换为print_r($res);
  • 打印 r 帖子和文件数组。有什么事吗?
  • @Alex 做了,但在控制台中我收到消息:发生错误

标签: php angular codeigniter file-upload upload


【解决方案1】:

您必须为此使用 php 输入流

$data = json_decode(file_get_contents('php://input'), true);

public function profile()   
{  
  $res = json_decode(file_get_contents('php://input'), true);
}

如果不清楚,请告诉我,因为我一直在控制器中使用它

【讨论】:

  • 那么我认为这是角度方面的问题,您可以检查您的浏览器控制台您在发布请求中发送的值。如果它没有从前端发送任何数据,那么您将不得不检查您的发送请求的角度服务或控制器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
相关资源
最近更新 更多