使用Angular/Rails发送带有params的http请求

Sending http requests with params using Angular/Rails

本文关键字:params http 请求 Angular Rails 使用      更新时间:2023-09-26

我在Angular中有一个前端应用程序,在Rails中有后端应用程序。为了便于维护,我将两者完全分离。

在Rails的后端,我有一个名为Document的模型,它有三个不同的参数(User_id(integer)、Data(json)和Title(string))。在我的前端,我有一个名为formData的变量,它是Angular中localStorage中的json数据。我不确定我是否正确地编写了在下面的HTTP请求中发送formData作为参数的代码。

这是我的请求:

$http.post('http://localhost:3000/documents', { user: $scope.user, formData: $scope.formData }, function (response) {
                $scope.isSaved = true;
                console.log(response);
                if (response.success) {
                    console.log("It has been successfully saved!")
                }
            });

我发送请求的来源是http://localhost:3001/i129。从这个网站,我需要提取用户所在的文档标题(即i129)和user_ID。我该怎么做?

我是否正确发送了上面的formData?假设

对于您想要的CRUD操作,我建议在Angular前端使用更高级别的工具,如$http之上构建的$resource服务。在这个链接上,你有一个关于如何使用它的解释,以及一个完整的电影示例,你可能会根据你的需求。

基本上,您可以配置后端API的端点,然后可以使用get()save()delete()等不错的方法,而不是httpAPI调用。

希望它能有所帮助!