使用AngularJS将数据发送到php脚本

使用AngularJS将数据发送到php脚本,angularjs,Angularjs,我是AngularJS新手,根据一些教程,我能够创建一个简单的页面和一个简单的表单 问题是每次我提交表单时,脚本似乎都在发送空数据 这是我的html: <div class="jumbotron text-center"> <h1>Contatti</h1> <p ng-show="message">{{ message }}</p> </div> <div class="r

我是AngularJS新手,根据一些教程,我能够创建一个简单的页面和一个简单的表单

问题是每次我提交表单时,脚本似乎都在发送空数据

这是我的html:

<div class="jumbotron text-center">
        <h1>Contatti</h1>

        <p ng-show="message">{{ message }}</p>
    </div>

<div class="row">
    <div class="col-lg-8">
        <form ng-submit="processForm()">
            <div class="form-group" ng-class="{ 'has-error' : errorName }">
                <label>Nome</label>
                <input type="text" name="name" class="form-control" placeholder="chris" ng-model="formData.name">
                 <span class="help-block" ng-show="errorName">{{ errorName }}</span> 
            </div>
            <button type="submit" class="btn btn-large">
                Salva
            </button>
        </form>
    </div>
</div>

<pre>
    {{ formData }}
</pre>
这是一个非常简单的php脚本:

Object {success: false, errors: Object}
errors: Object
name: "Name is required."
__proto__: Object
success: false
__proto__: Object
我错过什么了吗

TL;博士啊哈!既然
$scope.formData
是一个对象?然后执行
JSON.stringify()

TL;博士啊哈!既然
$errors
是数组?然后执行
json\u encode()

JS:

json_encode($errors);
write: JSON.stringify. json_encode()
read: JSON.parse(), json_decode()
PHP:

json_encode($errors);
write: JSON.stringify. json_encode()
read: JSON.parse(), json_decode()
注意:

$httpProvider.defaults.headers.common (headers that are common for all requests):
Accept: application/json, text/plain, * / *
$httpProvider.defaults.headers.post: (header defaults for POST requests)
Content-Type: application/json
$httpProvider.defaults.headers.put (header defaults for PUT requests)
Content-Type: application/json
对于,它在标题设置中默认(*注意:
dataType

设置HTTP标头

$http服务将自动将某些http头添加到所有 请求。可以通过访问 $httpProvider.defaults.headers配置对象,当前 包含此默认配置:


希望这有帮助。

您需要以php的形式获取数据

$http({
    url: "process.php",
    method: "POST",
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data: $.param($scope.formData)
}).success(function(data, status, headers, config) {

}).error(function(data, status, headers, config) {

});

添加jquery和 更改标题

$name= $_POST['name'];
php中

app.config(['$httpProvider' , '$routeProvider', function ($httpProvider, $routeProvider) {
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
}])

如果需要更改所有ajax请求的头,请在config block for EX中进行更改


此外,还可以更改AngularJS中的内容类型。如果我添加
标题
部分,则不会发生任何更改,同样的问题。如果我添加
$.param
我获取引用错误:$未定义如果我使用
文件获取内容
我获取
帖子http://localhost:8888/Angularjs/process.php 500(内部服务器错误)
its
file\u get\u contents
not
file\u get\u content
u missed last
s
我想:)
$。据我所知,param
是一个jQuery函数。因此,如果您在项目中没有jQuery,那么您可以尝试寻找替代品。类似这样:我已经更新了我的答案,包括以下原因;希望这有帮助。尝试打印所有帖子以查看错误。请显示
processForm()
@BrentWashburne的代码,只需滚动代码,您就会看到它。问题是在process.php中,$\u帖子总是empty@ChristianGiupponi,我更新了JSON的答案stringify(object),因为$scope.form是一个对象。看看情况如何。希望有帮助。
$name= $_POST['name'];
app.config(['$httpProvider' , '$routeProvider', function ($httpProvider, $routeProvider) {
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
}])