Angular 6 和 PHP:即使在使用附加到 httpParams 时,HttpClient 也不会将数组发送到服务器

编程入门 行业动态 更新时间:2024-10-23 03:29:36
本文介绍了Angular 6 和 PHP:即使在使用附加到 httpParams 时,HttpClient 也不会将数组发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我需要使用 Angular 6HttpClient 将一组数据发送到 PHP 服务器.

我有一些下拉列表供我选择并推入数组:

arrayOfLegalProtection=[];添加坐(){让 l_id = '';让 p_id = '';让 s_id = '';让 add_date = '';l_id = this.formGroup.controls['l_id'].value;p_id = this.formGroup.controls['p_id'].value;s_id = this.formGroup.controls['s_id'].value;add_date = this.formGroup.controls['add_date'].value;this.showSubHeader++;this.arrayOfLegalProtection.push({lId: l_id,pId: p_id,sId: s_id,添加日期:添加日期})控制台日志(this.arrayOfLegalProtection);}

控制台结果向我显示了正确的推送值:

<块引用>

[{"lId":"1","prId":"1","sId":"2","addDate":"2018-09-14"},

{"lId":"","pId":"1","sId":"5","addDate":"2018-09-14"},

{"lId":"","pId":"2","sId":"","addDate":"2018-09-20"}]

现在,我需要将此数组添加到 httpclient post 方法并将其发送到服务器,但正如讨论中所述

来自服务器的响应总是:

<块引用>

C:\wamp64\www\dev\UnitSaveData.php:23:string'[{"legalId":"","protectionId":"","situationId":"","addDate":"2018-09-14"},{"legalId":"","protectionId":"","situationId":"","addDate":"2018-09-14"}]'(长度=147)

但没有向数据库添加任何内容.

请注意,PHP 脚本与 之前未回复的帖子

解决方案

我找到了解决方案.

是的,HttpClient() 接受一个数组作为 HttpParams().

对于 Angular 脚本,我做了以下事情:

let httpParam = new HttpParams().set('unit_type', unit_type).set('location_type_id', location_type_id).set('user_id', user_id).set('number_of_hh', number_of_hh).set('unit_status',unit_status).set('add_date', add_date).set('arrayOfActualities', arrayOfActualities);

正如 HttpParams() 文档所提到的,.set() 将值作为一个字符串,即使在使用 json_encode() 时也会混淆服务器端脚本.

所以解决方案是将数组附加到arrayOfActualities:

let httpParam = new HttpParams().set('unit_type', unit_type).set('location_type_id', location_type_id).set('user_id', user_id).set('number_of_hh', number_of_hh).set('unit_status',unit_status).set('add_date', add_date).set('arrayOfActualities', arrayOfActualities);httpParam.append('arrayOfActualities', JSON.stringify(arrayOfActualities));

在 PHP 端,我需要使用 json_decode($arr, true),以便让服务器脚本遍历数组,因为遍历 json 数组给我带来了问题由于双引号无法读取字段标题:

$arr = json_decode($arrayOfActualities, true);如果(计数($ arr)> 0){foreach($arr 作为 $array){}}...

正如@B.Fleming 所说:

<块引用>

您的 PHP 脚本正在调用 json_encode(),它会生成一个 JSON细绳.如果您的意图是将 JSON 转换为数组,请执行以下操作json_decode().但是,在任何事情之前,请确保无论您是传入 json_decode() 还不是数组.

I need to send an array of data using HttpClient of Angular 6 to PHP server.

I have some drop down list where I chose from and push into an array:

arrayOfLegalProtection=[];
addSit(){
    let l_id = '';
    let p_id = '';
    let s_id = '';
    let add_date = '';
    l_id = this.formGroup.controls['l_id'].value;

    p_id = this.formGroup.controls['p_id'].value;
    s_id = this.formGroup.controls['s_id'].value;
    add_date = this.formGroup.controls['add_date'].value;
    this.showSubHeader++;
    this.arrayOfLegalProtection.push({lId: l_id, 
      pId: p_id,
      sId: s_id, 
      addDate: add_date})
    console.log(this.arrayOfLegalProtection);
}

The console result is showing me the correct pushed values:

[{"lId":"1","prId":"1","sId":"2","addDate":"2018-09-14"},

{"lId":"","pId":"1","sId":"5","addDate":"2018-09-14"},

{"lId":"","pId":"2","sId":"","addDate":"2018-09-20"}]

Now, I need to add this array to httpclient post method and send it to the server, but as said in the discussion of this post, arrays cannot be sent through angular's HttpClient library.

We need to use append, or JSON.stringify() or .append().

I posted a question after using the method of JSON.stringify() and as you can see there was errors. And even the echo count($array) is always 1 even if the multidimensional array contains 0 or N rows of data.

So I switched, for the other method using the .append():

saveUnitData(unit_type, 
    location_type_id, user_id, number_of_hh, unit_status, add_date, arrayOfActualities)
  { 
    console.log(user_id);
    let httpParam = new HttpParams().set('unit_type', unit_type)
                                      .set('location_type_id', location_type_id)
                                      .set('user_id', user_id)
                                      .set('number_of_hh', number_of_hh)
                                      .set('unit_status',unit_status)
                                      .set('add_date', add_date);
    Object.keys(arrayOfActualities).forEach(key=>{
      httpParam = httpParam.append('arrayOfActualities', arrayOfActualities);
    })
                                      //.set('arrayOfActualities', arrayOfActualities);
    let headerOptions = new HttpHeaders();
    headerOptions.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    console.log('h'+arrayOfActualities);
    return this.http.post(this.globalVar.UnitSavedataUrl, httpParam, {headers: headerOptions});
  }

Using this code:

Object.keys(arrayOfActualities).forEach(key=>{
          httpParam = httpParam.append('arrayOfActualities', arrayOfActualities);

I tried to convert an object into array using append so it can be sent through the httpClient service.

At the header of the request, we can see the ordinary data sent, but the array is being repeated hundreds and hundreds of times.

And the response from server is always:

C:\wamp64\www\dev\UnitSaveData.php:23:string '[{"legalId":"","protectionId":"","situationId":"","addDate":"2018-09-14"},{"legalId":"","protectionId":"","situationId":"","addDate":"2018-09-14"}]' (length=147)

But nothing was added to the database.

Please note that the PHP script is the same from the previous unanswered post

解决方案

I found the solution.

Yes, HttpClient() do accept an array as HttpParams().

For the Angular script, I was doing the following:

let httpParam = new HttpParams().set('unit_type', unit_type)
   .set('location_type_id', location_type_id)
   .set('user_id', user_id)
   .set('number_of_hh', number_of_hh)
   .set('unit_status',unit_status)
   .set('add_date', add_date)
   .set('arrayOfActualities', arrayOfActualities); 

As the HttpParams() documentation mentioned, .set() take the value as a string which confused the server side script even when using json_encode().

So the solution was to append to arrayOfActualities the array:

let httpParam = new HttpParams().set('unit_type', unit_type)
    .set('location_type_id', location_type_id)
    .set('user_id', user_id)
    .set('number_of_hh', number_of_hh)
    .set('unit_status',unit_status)
    .set('add_date', add_date)
    .set('arrayOfActualities', arrayOfActualities);

httpParam.append('arrayOfActualities', JSON.stringify(arrayOfActualities));

At the PHP side, I need to use json_decode($arr, true), in order to let the server script iterate over an array, as iterating over a json array was making problem for me as it wasn't able to read the field titles because of double quotes:

$arr = json_decode($arrayOfActualities, true);
if(count($arr)>0)
  {
     foreach($arr as $array)
     {

     }
  }

...

As, like @B.Fleming said:

Your PHP script is calling json_encode(), which produces a JSON string. If your intent is to transform JSON into an array, do json_decode(). Before anything, though, ensure that whatever you're passing into json_decode() is not already an array.

这篇关于Angular 6 和 PHP:即使在使用附加到 httpParams 时,HttpClient 也不会将数组发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 16:53:21,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也不   数组   发送到   会将   服务器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!