【问题标题】:How i can get data from json file using ajax我如何使用ajax从json文件中获取数据
【发布时间】:2018-10-22 15:04:08
【问题描述】:

我正在尝试在我的项目中实现 Ajax Long Polling 我的项目的这个结构 str 在 json 文件中我有这个代码

{"user": "user","two": "two","three": "three"}

当我尝试运行我的项目时,我得到了他的想法控制台:

WARN 8032 --- [nio-8080-exec-3].w.s.m.s.DefaultHandlerExceptionResolver:Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "status"

这在 chrome 控制台中: log

如果有任何帮助,我将不胜感激

【问题讨论】:

    标签: json ajax spring long-polling


    【解决方案1】:

    您的 javascript 文件不包含 jQuery 库。 您可以在引导库之前包含它(取决于 jQuery)。

    <script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
    

    【讨论】:

      【解决方案2】:

      根据此处提供的错误跟踪,您向status 字段提供了非数值,该字段不会转换为Long。由于这个NumberFormatException 提出。您可以在控制器中添加@ExceptionHandler,也可以使用@ControllerAdvice 定义全局异常处理程序来处理异常并发送适当的响应消息和状态代码。

      import org.springframework.http.HttpStatus;
      import org.springframework.http.ResponseEntity;
      import org.springframework.web.bind.MethodArgumentNotValidException;
      import org.springframework.web.bind.annotation.ControllerAdvice;
      import org.springframework.web.bind.annotation.ExceptionHandler;
      
      @ControllerAdvice
      public class ApiExceptionHandler {
          @ExceptionHandler(Exception.class)
          public ResponseEntity<Object> handlerGenericError(Exception ex){
              ex.printStackTrace();
              return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
          }
          @ExceptionHandler(MethodArgumentTypeMismatchException.class)
          public ResponseEntity<Object>handlerBadRequest(MethodArgumentTypeMismatchExceptionex){
              return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
          }
      }
      

      【讨论】:

      • 感谢您的帮助。这个解决方案让我从NumberFormatException 中解脱出来,但不幸的是没有解决我的问题
      猜你喜欢
      • 1970-01-01
      • 2021-01-25
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      相关资源
      最近更新 更多