简体   繁体   English

Angular的$ http.post到PHP空$ _POST

[英]Angular's $http.post to PHP empty $_POST

I have the following scenario, I have an Angular variable called contact which is a JSON object that has the following members: 我有以下情况,我有一个名为contact的Angular变量,它是一个具有以下成员的JSON对象:

  • Message 信息
  • Name 名称
  • Phone 电话
  • Email 电子邮件

I console.log(contact) and it logs the correct information, however once sent, $_POST['contact'] results in an empty array. console.log(contact)并记录了正确的信息,但是一旦发送, $_POST['contact']导致一个空数组。 I was reading around and found that $_POST['contact'] won't get populated like it would if it were an http request from jQuery. 我正在阅读,发现$_POST['contact']不会像是jQuery的http请求那样被填充。 So I fixed it to the following: 所以我将其固定为以下内容:

$params = json_decode(file_get_contents('php://input'),true);
print_r($params);

And here is the Angular function: 这是Angular函数:

$scope.submitForm = function(contact){
        console.log(contact);
        $scope.clearContactFormData();
        return $http.post('http://localhost/cpr_courses/app/methods/contact.php', contact).success(function(data){
            Materialize.toast(data, 4000);
        });
    }

Still the print_r($params) is empty. 仍然print_r($params)为空。 I have no idea what else is going on. 我不知道还有什么事。 Any help please? 有什么帮助吗?

EDIT 编辑

console.log() outputs the following: console.log()输出以下内容:

Array[0]
email
:
"asd@asd.asd"
length
:
0
message
:
"rubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOB"
name
:
"rubAWREIUVBAERWOBNATEIOB"
phone
:
"13345678"

and here is the network tab image: 这是网络标签图片:

响应头

Here's the declaration of the JSON object: 这是JSON对象的声明:

$scope.contact = {};
$scope.onlyNumbers = /^[0-9]+$/;

This is later put on on the form like this: 稍后将其放在这样的表单上:

<form method="post" class="form-sl" role="form" name="contactForm" ng-submit="submitForm(contact)" novalidate enctype="multipart/form-data">

and later on, on each field like this: 之后,在每个字段上都这样:

<input type="email" name="email" id="email" ng-model="contact.email" autocomplete="off" ng-maxlength="50" length="50" required/>

Is that okay? 这样可以吗?

The $_POST superglobal is a PHP commodity to automatically decode the body of the typical POST request sent by HTML forms. $_POST超全局变量是一种PHP商品,用于自动解码HTML表单发送的典型POST请求的正文。 In other words, it expects one of these values in the Content-Type request header: 换句话说,它期望Content-Type请求标头中的这些值之一:

  • application/x-www-form-urlencoded
  • multipart/form-data

Since you have application/json , it remains empty. 由于您具有application/json ,因此它保持为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM