本地主机/ci-abc/api/get_userid?username=yousuf
这是在邮递员中工作并返回用户ID的API
但是,我是一名初学者开发人员,所以我很困惑如何将 angular 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());
}
codeigniter 中的 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: "\"No username specified\"" headers: Object { _headers: Map, _normalizedNames: Map } ok: false status: 400 statusText: "Bad Request" type: 2 url: " http://localhost /ci-abc/api/get_userid " proto : Object { 构造函数: Response(), toString: Response.prototype.toString() }
HTTP GET
请求没有params
但是query 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');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句