duanmiyang6201 2019-01-14 18:28
浏览 60

对codeigniter的Angular post返回NULL

I'm new in Angular, and i'm developing a login auth service, but i'm gettin some troubles to make the post data for an backend developed in CodeIgniter.

I'm sending: username and userpassword, to URL: http://192.168.1.162/advance-managemente_2.1/login

Follow my code:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { UserData } from './login/user.model';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  constructor(private http: HttpClient) { }

  userData: UserData;

  getUserDetails(username, userpassword) {
    console.log(username, userpassword);
    const userdata = JSON.stringify({user: {login: username, password: userpassword}});
    const headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});

    console.log(userdata);
    // post these details to API server return user info if correct.
    return this.http.post(`http://192.168.1.162/advance-management_2.0/login`, userdata, {headers: headers, observe: 'response'})
    .subscribe(res => {
      console.log(res);
    },
    err => {
      console.error(err);
    });
  }
}

And my console: Note: my variables with username and userpassword is working.

And in codeigniter's backend project is setted these headers:

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');

And the function os codeigniter is:

function login(){
   $json = $this->input->post('user');
   echo json_encode($json);
}

If you observe, my login function have an ECHO with json_encode of i'm sending to her. But, in console, the body is returning NULL.

I'd like to know what I might be doing wrong, and how to fix it, because it was for him to return a JSON, but it does not seem to be identifying my POST.

Thanks to anyone who can help, Have a nice day!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!