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

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

问题描述

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

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

我有一个来自 .ts 类的方法:

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

我会在按钮的 (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);

并像这样格式化您的数据:

and format your data like that:

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天全站免登陆