发布数据 - ngResource AngularJS [英] post data - ngResource AngularJS

查看:139
本文介绍了发布数据 - ngResource AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果你好!
结果
我发展与AngularJS一个RESTful web应用程序,我使用了 ngResource 模块发送HTTP请求。该web服务与developped FuelPHP。

我有一个问题,保存 ngResource 的方法创建一个具有 $资源。我的Web服务不接收后的数据。

当我检查与萤火虫的HTTP请求,我可以看到POST数据。

我不明白,为什么后的数据不被web服务接收。所以,如果你有一个想法,它会很酷帮助我。

对不起,英文是我不好的水平。

下面是code:

 服务:
app.factory('药物',['$资源','全球',函数($资源,全球){
    返回$资源(global.API +'/药物/药物',{},{})
}])


 方法控制器:
$ scope.addMedication =功能(){
    VAR newMed =新的药物();
    newMed.name ='nameValue';
    newMed.increaseinr = 1;
    newMed.details ='detailsValue';
    。newMed $保存();
}


解决方案

我相信这是PHP是如何处理POST的问题。当使用AngularJS $资源将POST使用JSON对象视后的身体。 PHP不认为这是一个常规的参数。我不得不为此在其他PHP(从未使用过的燃料)

  $ requestBody =的file_get_contents('PHP://输入');
$ requestBody = json_de code($ requestBody,真正的);

那么你应该能够检查$ requestBody作为一个正常的JSON对象。


Hello !
I develop a RESTful webapp with AngularJS, I use the ngResource module to send http requests. The webservice is developped with FuelPHP.

I'm having a problem to creating a resource with the $save method of ngResource. My web service doesn't receive post data.

When I check the http request with Firebug, I can see the post data.

I don't understand why the post data are not received by the webservice. So if you have an idea, it would be cool to help me.

Sorry for my bad level in English.

Here is the code :

Service : 
app.factory('Medication', ['$resource', 'global', function ($resource, global) {
    return $resource(global.API+'/medication/medication', {}, {})
}])


Method in the controller : 
$scope.addMedication = function() {
    var newMed = new Medication();
    newMed.name = 'nameValue';
    newMed.increaseinr = 1;
    newMed.details = 'detailsValue';
    newMed.$save();
}

解决方案

I believe this is an issue with how PHP is handling the POST. When using AngularJS $resource it will POST the object with JSON as the post's BODY. PHP does not see this as a regular parameter. I've had to do this in other PHP (never used Fuel)

$requestBody = file_get_contents('php://input');
$requestBody = json_decode($requestBody, true);

Then you should be able to inspect $requestBody as a normal json object.

这篇关于发布数据 - ngResource AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆