简体   繁体   English

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

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

I'm trying to build small application. 我正在尝试构建小型应用程序。 It contains textarea and a button panel below wich is showing when I put first letter in this textarea. 它包含textarea,当我在该textarea中输入第一个字母时,该按钮下方会显示一个按钮面板。 I had to use isButtonVisible() method. 我不得不使用isButtonVisible()方法。 It works fine. 工作正常。 The problem starts when i click on "send" a request with data from textarea to data.php script by method POST (it's point of my task. it have to be send() method). 当我单击通过POST方法将带有从textarea的数据的请求“发送”到data.php脚本时,问题就开始了(这是我的任务。必须是send()方法)。 When request is done, script should show JavaScript alert() with "Data saved". 请求完成后,脚本应显示带有“数据已保存”的JavaScript alert()。

My angularjs script (ng-controller.js) looks like: 我的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;
        });
    };
};

You must define the use of '$http' inside the controller, then you can use it in the function. 您必须在控制器内部定义“ $ 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