douyong5476 2018-01-22 12:52
浏览 49

Angular 5和PHP无法使用POST方法向服务器脚本发送值

I am having a problem using post method in Angular 5 and PHP.

I have this method from a .ts class:

addPartners(partnerName)
{
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    this.name = JSON.stringify(partnerName)
    console.log("hi "+ this.name)
    return this.http.post('http://aff.local/addPartner.php', this.name, {
      observe: 'response',
      responseType: 'json'
    }).pipe(map(
    res=>{
      console.log(res)
    }
  ))
}

And I will call it on (click) event of a button:

addPartner(){
    this.email = this.subscribeForm.get('emailTxt').value;
    //console.log(this.email)
    this.api.addPartners(this.email).subscribe(
      (data)=>{
        console.log(data);
        this.subscribeForm.reset();
      },
      (error)=>{
        console.log(error)
      }
      );
}

The PHP script is :

addPartner($partnerName); echo $result; ?>

When I fill the textbox and click on the button, the value sent is empty.

When I change the method, by sending the variable in the url it work properly.

Here is the working script. In the api main class:

addPartners(partnerName)
  {
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    this.name = JSON.stringify(partnerName)
    console.log("hi "+ name)
    return this.http.post('http://aff.local/addPartner.php?name='+ name, {
      observe: 'response',
      responseType: 'json'
    }).pipe(map(
        res=>{
          console.log(res)
        }
      ))
  }

I just changed the url into:

http://aff.local/addPartner.php?name='+ name,

And in the php script I will get it using $_REQUEST['name'].

What I want is using the POST method because I need to send multiple data from a form.

  • 写回答

2条回答 默认 最新

  • dongza5150 2018-01-22 13:09
    关注

    In the POST-example you used this.name, but in the GET-example only name.

    Try to use $_POST['name'] instead of $_REQUEST['name], because $_REQUEST['name] is a different variable then $_POST or $_GET and also differently treated.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line