我是一个初学者。 我已经写在客户端和服务器端的PHP API做出了AngularJs GUI的测试应用程序。
这是角处理服务请求
myApp.factory('Book', ['$resource', 'API_URL', function($resource, API_URL){
return $resource(API_URL + '/books/:bookId', {bookId: '@bookId'}, {
get: { method: 'GET', isArray:true },
update: { method: 'PUT'},
save: { method: 'POST'},
delete: {method:'DELETE'},
});
}]);
当我从应用角度提交图书,我可以用捉修身POST
$post_a = json_decode($app->request->getBody());
//$post_b = $app->request->post(); //this would be empty
当我使用邮差和我进行POST我可以用捉修身POST
//$post_a = json_decode($app->request->getBody()); // this would be empty
$post_b = $app->request->post();
我不明白为什么会存在这种差异。 能否请您解释一下吗?
难道我不是为了赶上后只是$ APP->请求 - >后(); 在这两种情况下? 为什么从角来后只能用$ APP->请求 - > getBody被抓()?
的$app->request->post()
方法检索在提交键/值数据application/x-www-form-urlencoded
请求。 如果请求使用不同的内容类型(例如, application/json
),则可以检索与原始请求主体$app->request->getBody()
方法,并根据需要进行解码。 让我知道,如果你还有其他问题。
你仍然可以使用
$ post_b = $ APP->请求 - >柱()
在斯利姆。
只要你通过传递格式,而不是作为JSON数据的形式值调用从HTML表单(AngularJS)这个REST服务。 如果在AngularJS你有JSON格式的数据,你必须首先转换成表格。 下面是如何调用这个REST服务的例子:
Object.toparams = function ObjecttoParams(obj) { var p = []; for (var key in obj) { p.push(key + '=' + encodeURIComponent(obj[key])); } return p.join('&'); }; $http({ method: 'POST', url: url, data: Object.toparams(myobject), headers: {'Content-Type': 'application/x-www-form-urlencoded'} })
为MyObject是JSON格式,将要创建的数据
由于Josh..Your回答为我工作。
步骤如下:
1.You需要JSON格式发送请求原标签下是这样的:
{"username":"admin","password":"admin"}
2.You需要设置Content-Type
到application/json
的头。
这就是它,它会工作。
文章来源: Slim, Postman and AngularJs : $app->request->getBody() vs $app->request->post()