2017-12-18 226 views
0

本地主机/ CI-ABC/API/get_userid?用户名=优素福如何Angular4传递参数笨API

这是在邮递员的工作,并返回用户ID

然而,API我是一名初学者开发人员,所以我很困惑如何将角度http服务方法中的参数传递给codeigniter的API。没有参数要求的API工作正常,但我无法找到一种方法来传递用户名参数,以获得其ID。

我搜索了一下,能够找到URLSearchParams()作为解决方案,但它没有奏效,这就是我所做的。

getUserid() 
    { 
    let myParams = new URLSearchParams(); 
    myParams.append('username', 'yousuf'); 
    console.log(myParams); 
    return this.http.get('http://localhost/ci-abc/api/get_userid', {params : myParams}) 
    .map(res=>res.json()); 
    } 

和笨的API是

function get_userid_get() 
    { 
     $username = $this->get('username'); 

     if (!$username) { 
      $this->response("No username specified", 400); 
      exit; 
     } 
     $result = $this->user_model->get_userid($username); 
     if ($result) { 
      $this->response($result, 200); 
      exit; 
     } else { 
      $this->response("Invalid username", 404); 
      exit; 
     }} 

它已经几个小时,我尝试不同的方式,但没有运气如此虐待感谢帮助

Mozilla Firefox浏览器自带了这个错误的安慰。

错误{...} _body: “\” 没有指定用户名\ “” 头:对象{_headers:地图,_normalizedNames:地图}确定:假状态:400状态文本: “错误的请求” 型:2网址: “http://localhost/ci-abc/api/get_userid:对象{构造:响应(),的toString:Response.prototype.toString()}

回答

0

HTTP GET请求没有paramsquery strings

http.get('http://localhost/ci-abc/api/get_userid?' + myParams.toString()) 

如果您尝试进行身份验证,建议使用POST方法。

在你angular.js侧,

let myParams = { username: "username"}; 

http.post('http://localhost/ci-abc/api/get_userid', myParams) 

在您的API侧,

$username = $this->input->post('username'); 
+0

我也在思考这个解决办法,但我不是肯定的,如果这是因为它看起来做的正确方法就像即时编码查询参数。 –

+0

这是传递查询参数的唯一方法吗? –

+0

@YousufKhan尝试'http.post()'而不是'http.get()'。 – adeltahir