doujian1050 2015-01-19 10:04
浏览 41

我不能通过POST方法从angularjs发送请求到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;
        });
    };
};
  • 写回答

1条回答 默认 最新

  • douyi1899 2015-01-19 10:17
    关注

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料