在 Rest Api 中调用 GET 并发送 POST 以向 Rest Api 提供信息
Call a GET in a Rest Api and send POST to give informations to the Rest Api
我目前正在使用 Symfony 和 FosRestBundle 创建一个 Api。
在 symfony 中,我想捕获来自客户端的请求以返回一些信息。 (所以一个 get 方法)。
这是我的代码,允许 api 和客户端之间的通信。
Api 边 :
public function getPropertiesAction(Request $request){
$properties = $this->getDoctrine()->getManager()->getRepository('ApiBundle:Achats')->findAll();
if(null == $properties){
throw $this->createNotFoundException();
}
$data = findPropertiesInside($properties, $request);
return $data;
}
客户端:
angular.module('googleMapApp')
.service('PropertiesData', function ($http) {
this.fetch = function(url, polygone, callback, errorCallback) {
$http.post(url,{data: polygone}).then(callback,errorCallback);
};
});
所以当我这样做时 api return 错误 405(方法不允许)
有没有办法将客户端的postjson信息发送给api和apireturn好的属性?
使用 HTTP GET 时不能传递请求正文。您应该使用路径或查询字符串在 url 中传递数据。
将您的客户端更改为 $http.get(urlWithData)。然后...
我目前正在使用 Symfony 和 FosRestBundle 创建一个 Api。
在 symfony 中,我想捕获来自客户端的请求以返回一些信息。 (所以一个 get 方法)。
这是我的代码,允许 api 和客户端之间的通信。
Api 边 :
public function getPropertiesAction(Request $request){
$properties = $this->getDoctrine()->getManager()->getRepository('ApiBundle:Achats')->findAll();
if(null == $properties){
throw $this->createNotFoundException();
}
$data = findPropertiesInside($properties, $request);
return $data;
}
客户端:
angular.module('googleMapApp')
.service('PropertiesData', function ($http) {
this.fetch = function(url, polygone, callback, errorCallback) {
$http.post(url,{data: polygone}).then(callback,errorCallback);
};
});
所以当我这样做时 api return 错误 405(方法不允许)
有没有办法将客户端的postjson信息发送给api和apireturn好的属性?
使用 HTTP GET 时不能传递请求正文。您应该使用路径或查询字符串在 url 中传递数据。
将您的客户端更改为 $http.get(urlWithData)。然后...