我正在使用FosRestBundle API,我必须在url post资源中传递params来执行操作。
FosrestBundle中的帖子资源是这样的:
api/readQuestion/{contain_id}/{user_id}/{type_id}/Questions
所以要在我的angularjs应用程序中使用该资源发送数据。我以这种方式创建了一个服务:
angular.module("QuestionRest", ['ngResource'])
.factory("question", function ( $resource) {
var apiData = $resource(
"/api", {},
{
"postQuestion" : { method: "POST", url: "/api/readQuestion/:containid/:userid/:typeid/Questions"}
});
return {
postQuestion: function(newQuestion, contain_id, user_id, type_id) {
apiData.postQuestion(newQuestion, {containid: contain_id, userid: user_id, typeid: type_id}, function() {
console.log("Success !");
}, function(error) {
console.log("Error " + error.status + " when sending request : " + error.data);
});
}
}
});
但它根本不起作用。当我在我的app控制器中调用postQuestion时:
question.postQuestion(newQuestion, 1 ,1 ,1 ),
我收到了这个错误:
angular.js:12410 POST http://localhost:8000/api/readQuestion/Questions?description=%3Cp%3Efdfdf%3C%2Fp%3E&title=fdf%3F&line=1000&page=1000 404 (Not Found)
其中:
{
"title": "fdf?",
"description": "Cp...3Efdfdf...",
"line": "1000",
"page": "1000"
}
是我的帖子请求发送的newQuestion JSON对象。
你知道我的意思。
在角度应用帖子网址中传递参数的最佳方法是什么?
答案 0 :(得分:2)
参数和数据相反:
可以使用以下参数调用类对象或实例对象上的操作方法:
- 非GET" class" actions:Resource.action([parameters],postData,[success],[error])
postQuestion: function(newQuestion, contain_id, user_id, type_id) {
//apiData.postQuestion(newQuestion, {containid: contain_id, userid: user_id, typeid: type_id}, function() {
var params = {containid: contain_id, userid: user_id, typeid: type_id};
apiData.postQuestion( params, newQuestion, function() {
console.log("Success !");
}, function(error) {
console.log("Error " + error.status + " when sending request : " + error.data);
});
}