2017-12-18 218 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