首页 文章

在Angular 4调用的CodeIgniter API中,input-> post和$ _POST是空的,在angular 4中发出post请求的正确方法是什么

提问于
浏览
0

这是我第一次从Angular向CodeIgniter rest API发出post方法请求 .

postUsertask(Userid,TaskName)
  {
    let body ={
      userid:Userid, taskname:TaskName
    };  
    console.log(body);
    return this.http.post("http://localhost/ci-abc/api/add_task",JSON.stringify(body) )
    .map(res => res.json());
  }

codeigniter中的API方法:

function add_task_post()
{
    $obj=json_decode(file_get_contents('php://input'));
    $taskname = $obj->taskname;
    $userid = $obj->userid;
    if (!$taskname || !$userid) {
        $this->response("Enter taskname and userid to add", 400);
    } else

        $result = $this->todo_model->add_task($taskname, $userid);

    if ($result === 0) {
        $this->response("Task could not be added. Try again.", 404);
    } else {
        $this->response("success", 200);
    }
}

不得不包括访问数据

$ obj = json_decode(file_get_contents('php:// input'));

因为$ this-> input-> post和$ _POST是空的,并且从angular收到的数据是一个对象,所以必须用 - >表示法访问 . 我很好奇这不是正确和道德的方式来做到这一点 . 此外,当我没有放JSON.stringify时,它给了我Cross Origin Request阻止错误,这就是我把它放的原因 . 我应该如何在angular4中使用angular4中的POST和PUT请求来停止API?

如何摆脱不让我调用API方法的CORS错误,如果我可以摆脱CORS错误,那么我也可以删除 JSON.stringify ,它将按原样发送数据,我相信数据应该通过 input->post$_POST .

编辑2:在进行POST PUT和DELETE API调用时出现这些错误 .

阻止跨源请求:同源策略不允许在http:// localhost / ci-abc / api / del_task?taskid = 34读取远程资源 . (原因:CORS预检 Channels 没有成功)

1 回答

  • 1

    EDIT (Perfect Solution):

    发现formdata对象方法已被弃用,因此我只在选项中包含了一个标头,并包含在API调用http.post方法中,该方法工作正常并且是更好的解决方案 .

    constructor(public http:Http) { 
    let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        let options = new RequestOptions({ headers: headers });}
    
    createUser(userName)
      {
        let body = { username:userName};
        return this.http.post("http://localhost/ci-abc/api/create_user",body,this.options)
        .map(res => res.json());
      }
    

    Deprecated approach (Works but deprecated/not usual practice):

    花了几个小时但找到了解决方案,我创建了body作为一个新的formdata对象,将其作为键及其值附加参数,现在我正在通过$ this-> input-> post检索它 .

    let body = new FormData;
        body.append('userid', Userid);
        body.append('taskname', TaskName);
        console.log(body);
        return this.http.post("http://localhost/ci-abc/api/add_task",body)
        .map(res => res.json());
    

    在我的codeigniters API控制器的构造函数中使用这些头文件

    header("Access-Control-Allow-Origin: *");
    header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin');
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
    

    API方法:

    function add_task_post()
        {
            $userid = $this->input->post('userid');
            $taskname = $this->input->post('taskname');
    
            if (!$taskname || !$userid) {
                $this->response("Enter taskname and userid to add", 400);
            } else
                $result = $this->todo_model->add_task($taskname, $userid);
            if ($result === 0) {
                $this->response("Task could not be added. Try again.", 404);
            } else {
                $this->response("success", 200);
            }
        }
    

相关问题