Angular 5 and PHP can't use the POST method to send a value to server scripts

人盡茶涼 提交于 2019-12-13 03:44:00

问题


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.


回答1:


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

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



回答2:


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.



来源:https://stackoverflow.com/questions/48382062/angular-5-and-php-cant-use-the-post-method-to-send-a-value-to-server-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!