Php $http post和获取AngularJS
我现在正在学习AngularJS 我试图用$http服务创建一些东西 每次我发送数据时,都会将其作为post或get发送 服务器端(PHP)告诉我它是一个空数据(NULL) 这是到目前为止我的代码。Php $http post和获取AngularJS,php,angularjs,ajax,xmlhttprequest,Php,Angularjs,Ajax,Xmlhttprequest,我现在正在学习AngularJS 我试图用$http服务创建一些东西 每次我发送数据时,都会将其作为post或get发送 服务器端(PHP)告诉我它是一个空数据(NULL) 这是到目前为止我的代码。 我想知道为什么它总是接收一个空的数据字符串,并对作为json或普通数据属性发送的数据进行编码,如PHP=>(name=ahmed&job=student) Controller.js 请注意,当您从常规表单、Ajax或jQueryAjax向其发送数据时,以下PHP代码工作正常,没有任何问题 newA
我想知道为什么它总是接收一个空的数据字符串,并对作为json或普通数据属性发送的数据进行编码,如PHP=>(name=ahmed&job=student) Controller.js 请注意,当您从常规表单、Ajax或jQueryAjax向其发送数据时,以下PHP代码工作正常,没有任何问题 newArticle.php
index.html
第条主题:
文章内容:
{{show}
我认为主要的问题是,您有一个表单正在对后端的PHP脚本进行提交,但您正在尝试使用angular。我的猜测是,形式动作发生在angular做它的事情之前。如果您使用Angular,则不希望进行表单提交。Angular总是想用AJAX和JSON与服务器对话。我明白了,我现在就更改它,让您知道发生了什么事情。使用表单没有什么问题。从单击按钮更改为提交。从窗体中删除操作。另外请注意,默认的预期响应是jsonI。我已经尝试完全删除表单标记,但没有任何效果(相同的问题),我也按照您所说的做了,并按照建议添加了ng submit,但没有任何效果(相同的问题)@charlietfl您可以详细说明“默认的预期响应是json”吗
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");
});
};
});
<?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";
}
}
?>
<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>