【问题标题】:How to send multiple variables to PHP using an AngularJS Ajax call?如何使用 AngularJS Ajax 调用将多个变量发送到 PHP?
【发布时间】:2023-03-03 18:23:01
【问题描述】:

我正在学习 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 做到这一点?可能吗?

【问题讨论】:

标签: javascript php jquery angularjs ajax


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以使用 $http 和 $resource 的 angular 服务。我主要使用 $http。您可以在 $http 的数据或参数属性中在 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
         });
    

    【讨论】:

      猜你喜欢
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多