簡體   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