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

问题描述

我需要使用 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:

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:

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:

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

10-22 20:05