繁体   English   中英

我无法通过POST方法将请求从angularjs发送到php

[英]I can't send a request by POST method from angularjs to php

我正在尝试构建小型应用程序。 它包含textarea,当我在该textarea中输入第一个字母时,该按钮下方会显示一个按钮面板。 我不得不使用isButtonVisible()方法。 工作正常。 当我单击通过POST方法将带有从textarea的数据的请求“发送”到data.php脚本时,问题就开始了(这是我的任务。必须是send()方法)。 请求完成后,脚本应显示带有“数据已保存”的JavaScript alert()。

我的angularjs脚本(ng-controller.js)如下所示:

angular.module("Textarea", []).controller("TextAreaCtrl", TextAreaCtrl);

function TextAreaCtrl($scope, $http)
{
    'use strict';

    $scope.success = false;
    $scope.httpError = false;

    this.isButtonVisible = function()
    {
        return $scope.text;
    };

    this.send = function ()
    {
        var data = $scope.text;

        if(!data)
            return false;

        $http.post('data.php', data)
        .success(function (data)
        {
            $scope.success = true;
        })
        .error(function (data)
        {
            $scope.httpError = true;
        });
    };
};

您必须在控制器内部定义“ $ http”的用法,然后才能在函数中使用它。

angular.module("Textarea", []).controller("TextAreaCtrl", ['$scope', '$http', function($scope, $http) {
  $scope.orderList = function() {
    $http.post('http://example.com').
      success(function(data) {
        $scope.data = data;
      }).
      error(function(data, status) {
        console.log(data);
        console.log(status);
      });
    };
}];

暂无
暂无

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

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