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

问题描述

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

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":","pId":"1","sId":"5","addDate":"2018-09-14"},

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

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

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

现在,我需要将此数组添加到httpclient post方法并将其发送到服务器,但是正如讨论中所说这篇文章,无法通过angular的HttpClient库发送数组.

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.

我们需要使用append或JSON.stringify().append().

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

我,您可以看到其中存在错误.即使多维数组包含0或N行数据,即使echo count($array)始终也是1.

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.

所以我换了另一种使用.append()的方法:

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});
  }

使用此代码:

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

我尝试使用append将对象转换为数组,以便可以通过httpClient服务发送该对象.

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.

服务器的响应始终是:

但是什么都没有添加到数据库中.

But nothing was added to the database.

请注意,PHP脚本与

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

推荐答案

我找到了解决方案.

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

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

对于Angular脚本,我正在执行以下操作:

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); 

HttpParams()文档所述,.set()将该值作为字符串使用,即使使用json_encode()时,该字符串也会混淆服务器端脚本.

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

因此解决方案是将数组追加到arrayOfActualities:

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));

在PHP端,我需要使用json_decode($arr, true),以便让服务器脚本在数组上进行迭代,因为在json数组上进行迭代对我来说是个问题,因为它无法读取字段标题由于双引号:

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)
     {

     }
  }

...

就像@ B.Fleming说的那样:

As, like @B.Fleming said:

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

11-02 11:36