【问题标题】:how to send form data of array from angular to php script如何将数组的表单数据从角度发送到php脚本
【发布时间】:2020-08-08 05:07:55
【问题描述】:

我无法将数组列表发送到 PHP 脚本以获取数据列表。

以下是我的角度服务。

sendFormData(userData) {
console.log('hi');
console.log(userData);
var data = [];
data.push(userData);
console.log(data); 

return this.http.post("http://localhost:8088/post_candidateDetails.php", data);

} 

PHP 脚本

<?php

header("Access-Control-Allow-Origin: http://localhost:4200");
header("Access-Control-Allow-Credentials: true ");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: X-Custom-Header, Origin, Content-Type , Authorisation , X-Requested-With");
header("Content-Type: application/json; charset=UTF-8 ");
print_r($_REQUEST);

?>

我的浏览器中出现空白输出数组。

Array
(
)

【问题讨论】:

    标签: php angular multipartform-data form-data


    【解决方案1】:

    在Angular中发送表单数据的情况下,$http POST数据信息被序列化为application/jsonPHP无法解释。

    您必须将请求(表单数据对象)转换为它可以理解的格式。最简单的方法是配置 $httpProvider。

    myApp.config(['$httpProvider', '$httpParamSerializerProvider', function($http, $httpParamSerializerProvider) {
        var paramSerializer = $httpParamSerializerProvider.$get();
        $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        $http.defaults.transformRequest = function(data) {
            return paramSerializer(data);
        };
    }]);
    

    taken from here

    a details example

    【讨论】:

    • 我正在使用 Angus 新版本 8 可以更新完整答案我正在使用这个 url:localhost:8088/post_candidateDetails.php。 @弗罗斯特
    • @mohdmazharkhan 这与角度无关 - 您的角度正在发送数据,但 php 无法接收。更新你的 php 数据接收函数
    • @mohdmazharkhan 如果您需要详细了解某些内容,这可能会对您有所帮助phpenthusiast.com/blog/…
    • 我必须在上面的答案代码中添加这个 url:localhost:8088/post_candidateDetails.php,我对 angular 很陌生。@Frost
    • 你的回答对我没有帮助。@Frost
    猜你喜欢
    • 2019-04-03
    • 2012-06-15
    • 2012-01-06
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2012-03-14
    相关资源
    最近更新 更多