简体   繁体   中英

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. I had to use isButtonVisible() method. 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). When request is done, script should show JavaScript alert() with "Data saved".

My angularjs script (ng-controller.js) looks like:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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