angular:使用表单提交传递其他数据

时间:2017-01-16 18:17:47

标签: angularjs

目前我正在使用下面的代码并且工作正常。

JsonSlurper

现在我有一个小要求。我需要传递用户ID( uid )和表单数据。 在Jquery中它很简单,但我是棱角分明的新手并没有多少经验。

任何建议如何在Angular中传递其他数据。

谢谢

2 个答案:

答案 0 :(得分:0)

你可以在$ http数据参数中发送一个对象(php中的关联数组):

$scope.processForm = function($scope.formData) {
    console.log($scope.formData);
    $http({
        method  : 'POST',
        url     : 'process.php',
        data    : {'data': $scope.formData,'userid':userid}, // no need to use $.param, even never see it in angular
        headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
    })

有关如何在php端获取数据的详细信息,请参阅https://scotch.io/tutorials/submitting-ajax-forms-the-angularjs-way

答案 1 :(得分:0)

您可以使用uid参数将formData封装在对象expl中:

var allData={'formData': $scope.formData, 'uid': $scope.uid}

然后将allData对象传递给post方法数据: allData

如果您在process.php(例如未定义的数据)中获取数据时遇到任何问题,您应该让Angular传递内容类型为application / json(angularjs的默认行为)的数据,并在process.php中传递数据像这样:

$postContent= file_get_contents("php://input"); 
$req= json_decode($postContent); 
$formData= $req->formData; 
$uid= $req->uid;

请注意,如果先测试一些静态值('uid':2)以确保它正常工作,那将会很棒