$ http发布并加入AngularJS

时间:2016-02-27 01:15:36

标签: php angularjs ajax xmlhttprequest

我正在学习AngularJS 我正在尝试用$ http服务创建一些东西 每当我发送我的数据时,无论是发布还是获取 服务器端(PHP)告诉我它是一个空数据(NULL) 到目前为止,这是我的代码 我想知道为什么它总是收到一个空的数据字符串,以及Angular代码作为json或普通数据属性发送的数据,如PHP => (名称=艾哈迈德&安培;作业=学生)

Controller.js

var app = angular.module('AddArticle', []);
app.controller("AddArticlesController", function($scope, $http, $httpParamSerializerJQLike) {
    $scope.SubmitData = function() {

        $http({
            url: 'Inc/newArticle.php',
            method: 'POST',
            params: $httpParamSerializerJQLike({ArticleSubject: $scope.ArticleSubject, ArticleContent: $scope.ArticleContent}),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(function(response) {
            if (response.data == "yes") {
                $scope.show = "Sent";
            } else {
                alert(response.data);
            }
        }, function(response) {
            alert("failed php");
        });
    };
});

请注意,当您从常规表单,Ajax或JQuery Ajax发送数据时,以下PHP代码可以正常工作而没有任何问题

newArticle.php

    <?php 
        require_once $_SERVER['DOCUMENT_ROOT'] . "HardWork Results/AngularJS/Inc/functions.php";
// the following two echo are for testing onle
        echo $_POST["ArticleSubject"];
        echo $_POST["ArticleContent"];
        $Subject = htmlspecialchars($_GET["ArticleSubject"]);
        $Content = htmlspecialchars($_GET["ArticleContent"]);
        if (empty($Subject)) {
            echo "Subject is null";
        }
        if (empty($Content)) {
            echo "Content is null";
        }
        if (!empty($Subject) && !empty($Content)) {
            if (AddArticle($Subject, $Content) ) {
                echo "yes";
            } else {
                echo "no";
            }
        }
    ?>

的index.html

<div class="Article" data-ng-app="AddArticle" data-ng-controller="AddArticlesController">
    <form name="Articles" method="post" data-ng-submit="SubmitData()">
        <div class="form-group">
            <label>Article Subject: </label>
            <input type="text" required id="ArticleSubject" name="ArticleSubject" placeholder="ex: Moussa is shitty TA" data-ng-model="ArticleSubject" />
        </div>
        <div class="form-group">
            <label>Article Content: </label>
            <textarea rows="10" cols="50" required id="ArticleContent" name="ArticleContent" placeholder="ex: We know Moussa is Awesome, have fun and enjoy life" data-ng-model="ArticleContent"></textarea>
        </div>
        <button type="submit" class="submit" data-ng-click="SubmitData()" data-ng-init="show='Send'">{{ show }}</button>
    </form>
    <div class="ArticleDesign">
        <h1 data-ng-bind="ArticleSubject"></h1>
        <p data-ng-bind="ArticleContent"></p>
    </div>
</div>

0 个答案:

没有答案