【问题标题】:GET Service call is not getting called from ControllerGET 服务调用未从 Controller 调用
【发布时间】:2019-04-20 07:41:33
【问题描述】:

我正在尝试将 Angular 引入我的旧应用程序之一。但不确定为什么这不调用服务。下面是 JS 文件中的代码。早些时候我在浏览器中遇到错误,说 $http 无法解决。所以我只是在函数中传递了它。

var app = angular.module('ldlApp', []);
app.controller('ldlController', function($scope,$http) {
  console.log(" Inside Controller **** ");
  $scope.message = 'Hello from LDL Controller !!!! ';
  $scope.BLT_LDL_DECESION_LOAN_DATA = [];
  console.log(" Going to Hit the Service ***** ");
  $http.get("/Services/ldl/getdetails")
    .then(function(response) {
      $scope.BLT_LDL_DECESION_LOAN_DATA = response.data;
      console.log("BLT_LDL_DECESION_LOAN_DATA: 
            "+JSON.stringify($scope.BLT_LDL_DECESION_LOAN_DATA));
      });
  });

下面是java文件中的REST控制器

@RestController
public class LoanDecesionController {

   @RequestMapping(value = "/Services/ldl/getdetails", method = RequestMethod.GET)  
   @ResponseBody
   @Transactional
   public List<LinkedHashMap<String, Object>> getdetails() throws Exception {
      System.out.println(" Inside Service **** ");
      List<LinkedHashMap<String, Object>> dataMap = new ArrayList<LinkedHashMap<String, Object>>();
      LinkedHashMap<String, Object> responsedataMap = new LinkedHashMap<>();
      responsedataMap.put("SUCESS", "Called the service ");
      dataMap.add(responsedataMap);
      return dataMap; 
   }

}

在浏览器中,我可以看到类似

的消息
Inside Controller **** 
Going to Hit the Service ***** 

以下是我在网络选项卡中看到的内容。

Request URL: https://*****-*****-*****.com/Services/ldl/getdetails
Request Method: GET
Status Code: 302 Found
Remote Address: 10.***.***.49:553
Referrer Policy: no-referrer-when-downgrade

但我没有在控制器中获取系统输出。那么问题是真的出在响应还是服务上。

【问题讨论】:

  • 请缩进你的代码,它不可读。
  • 并使用调试工具(特别是浏览器的网络选项卡)查看请求是否正确传输。
  • 正如@chrylis 所说,检查网络选项卡。我猜你的请求没有返回 HTTP 200,所以 then 不会被触发。 then 仅在服务器成功响应时触发,如果服务器返回错误 HTTP 返回代码,请尝试添加 catch 以打印。
  • 用我在浏览器网络选项卡中看到的内容更新了问题
  • 你会得到一个 302,这是一个重定向。在答案中必须有一个 Location 标头给出重定向指向的 url。你必须提取它并向这个 url 发布一个新请求。

标签: javascript java angularjs spring


【解决方案1】:

当使用 @Transactional@ResquestMaping spring boot 时不要自动配置 URL 映射。从您的方法中删除 @Transactional 并尝试在代码中的其他位置管理事务

【讨论】:

    【解决方案2】:

    看起来我现有的应用程序正在阻止 URL 并且不允许访问任何其他 URL。感谢大家的帮助和对问题的深入了解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-27
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 2019-05-15
      • 2017-10-15
      • 1970-01-01
      相关资源
      最近更新 更多