繁体   English   中英

如何使用 AngularJS Ajax 调用将多个变量发送到 PHP?

[英]How to send multiple variables to PHP using an AngularJS Ajax call?

我正在学习 angularJS,我想知道如何像使用 Jquery 一样通过 Angular AJAX 发送多个数据变量?

这就是我用 Jquery 做到的:

$.post("eventCreate.php",
                        {
                            daySelect: daySelect,
                            startTime: startTime,
                            endTime: endTime,
                            eventName: eventName,
                            loginName: "<?php echo $user; ?>",
                            sliderName: sliderName
                        },
                        function(result)
                        {
                          ;
                        });

那么我如何用 Angularjs 做到这一点呢? 有可能吗?

您可以使用angular的$http和$resource服务。我主要使用$http。您可以在数据或$http的params atttibute中的angular js ajax调用中发送多个变量,如下所示

$http({
    url: user.details_path, 
    method: "GET",
    params: {user_id: user.id,user_name:user.name}
 });

 $http({
        url: user.details_path, 
        method: "GET",
        data: userObject
     });

在 AngularJs 中,我们创建Controllers并在其中做我们的事情。 AngularJs 也支持POSTGET方法。 在这段代码中,我给你一个POST方法的演示。 根据您更改variable name ,您也可以增加和减少它们。

在服务器端使用$_POST来获取这些值。

$scope.YourControllerName = function () {
    var req = {
            method: 'POST',
            url: 'YourURL',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: {
                userId: userId,
                userName: userName, // Or anything you want to send
                action: "addUser"  //Suppose you are adding a user
               },
            }

            $http(req).then(function(response){
                //This will called on success do something with this response
            }, function(response){
                //This will called on failure do something with this response
            });
        };

暂无
暂无

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

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