Angular 5和PHP不能使用POST方法将值发送到服务器脚本 [英] Angular 5 and PHP can't use the POST method to send a value to server scripts

查看:79
本文介绍了Angular 5和PHP不能使用POST方法将值发送到服务器脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 5和PHP中使用post方法时遇到问题.

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

我在.ts类中有此方法:

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

我将在按钮的(click)事件中调用它:

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

PHP脚本为:

当我填充文本框并单击按钮时,发送的值为空.

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

当我更改方法时,通过在url中发送变量,它可以正常工作.

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

这是工作脚本.在api主类中:

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

我刚刚将网址更改为:

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

在php脚本中,我将使用$_REQUEST['name']来获取它.

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

我想要的是使用POST方法,因为我需要从表单发送多个数据.

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

推荐答案

如果要使用JSON.stringify,则可以使用以下命令从服务器端获取参数:

if you want to use JSON.stringify so you can take params from the server side using :

$request_body = file_get_contents('php://input');
$data = json_decode($request_body);

或者如果我们只想使用$ _POST ['paramName']来获取数据,那么我们必须在您的客户端中使用以下内容:

or if we want to just take data using $_POST['paramName'] so we have to use the below in your client side:

let headerOptions = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
let data = {name: partnerName};
let body = this.formatData(test);

并像这样格式化数据:

formatData(data) {
    let returnData = '';
    let count = 0;
    for (let i in data) {
      if (count == 0) {
        returnData += i + '=' + data[i];
      } else {
        returnData += '&' + i + '=' + data[i];
      }
      count = count + 1;
    }
    return returnData;
  }

这篇关于Angular 5和PHP不能使用POST方法将值发送到服务器脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆