te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How can I get HttpParams values as JSON object? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I get HttpParams values as JSON object? - Stack Overflow

programmeradmin2浏览0评论

I'm using Angular 5 with HttpInterceptors.
I already know I can get each value I want from HttpParams via several methods.

Also - If I want to see all values , I can use the .toString() method

params = new HttpParams()
    .set('page', '2')
    .set('sort', 'name');

console.log(params.toString()); //Returns page=2&sort=name

But in my case I send json objects as parameters :

{
  a:1 , b:[1,2,3] , c:[{...}]
}

I'm using interceptors to log the request parameters , but when I JSON.stringify(req.Params) , I get :

Params={
    "updates": null,
    "cloneFrom": null,
    "encoder": {},
    "map": {}
}

Which doesn't expose the values.

I don't want to see the parameters as a regular form post parameters -( it will be very unclear), but as an object as I've sent it.

Question:

How can I extract the parameters from the request object in the interceptor , but as json format :

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
    {
       JSON.stringify( req.params)  // <--- ?? doesn't yield the params.
   }

I'm using Angular 5 with HttpInterceptors.
I already know I can get each value I want from HttpParams via several methods.

Also - If I want to see all values , I can use the .toString() method

params = new HttpParams()
    .set('page', '2')
    .set('sort', 'name');

console.log(params.toString()); //Returns page=2&sort=name

But in my case I send json objects as parameters :

{
  a:1 , b:[1,2,3] , c:[{...}]
}

I'm using interceptors to log the request parameters , but when I JSON.stringify(req.Params) , I get :

Params={
    "updates": null,
    "cloneFrom": null,
    "encoder": {},
    "map": {}
}

Which doesn't expose the values.

I don't want to see the parameters as a regular form post parameters -( it will be very unclear), but as an object as I've sent it.

Question:

How can I extract the parameters from the request object in the interceptor , but as json format :

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
    {
       JSON.stringify( req.params)  // <--- ?? doesn't yield the params.
   }
Share Improve this question asked Feb 22, 2018 at 15:56 Royi NamirRoyi Namir 149k144 gold badges492 silver badges829 bronze badges 12
  • can you post how you add those objects to the HttpParams object? – messerbill Commented Feb 22, 2018 at 16:00
  • @messerbill SURE i.imgur./Sgn3Ak8.jpg – Royi Namir Commented Feb 22, 2018 at 16:03
  • 2 That's because they are lazy. See also stackoverflow./questions/46418746/… – yurzui Commented Feb 22, 2018 at 16:04
  • @yurzui Hi. If so , how can I eagerly extract them as Json.stringify ? – Royi Namir Commented Feb 22, 2018 at 16:05
  • @RoyiNamir This helps 0. – messerbill Commented Feb 22, 2018 at 16:07
 |  Show 7 more ments

2 Answers 2

Reset to default 8

If you don't like method toString() that returns an encoded string, where key-value pairs (separated by =) are separated by &s you can write your own method that will transform data stored in Map in some data you want to use.

For example:

const params = new HttpParams()
  .set('page', '2')
  .set('sort', 'name');

const paramsArray = params.keys().map(x => ({ [x]: params.get(x) }));

console.log(JSON.stringify(paramsArray));

It's similar to the approach that is used in toString method https://github./angular/angular/blob/master/packages/mon/http/src/params.ts#L177-L186

Ng-run Example

What you actually requested was a JSON Object. So this is how you get such an object:

const params = new HttpParams()
  .set('page', '2')
  .set('sort', 'name');

const paramsObject = params.keys().reduce((object, key) => {
    object[key] = params.get(key)
    return object
}, {})
console.log(paramsObject)
// And json if you really want
const json = JSON.stringify(paramsObject)
发布评论

评论列表(0)

  1. 暂无评论