【问题标题】:SyntaxError : Json.parse:unexpected end of data GWTSyntaxError : Json.parse:unexpected end of data GWT
【发布时间】:2017-03-01 12:15:37
【问题描述】:

当我使用 GWT 向我的 API 发出休息请求时,我在客户端收到 “Json.parse:unexpected end of data”。

客户端代码:

private void executeUnAccountedTransactionByService(String requestJson)
    {
        String requestData = "request=" + URL.encodeQueryString(requestJson);

        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(postPaymentUrl));

        builder.setHeader("Content-Type","application/x-www-form-urlencoded");
        builder.setHeader("Accept","application/json") ;
        try  {

            builder.sendRequest(requestData, new RequestCallback()  {

                @Override
                public void onResponseReceived(Request request, Response response)  {

                    JSONValue jsonValue = JSONParser.parseStrict(response.getText());
                    JSONObject responseObject = jsonValue.isObject();
                    //my logic
                }

                @Override
                public void onError(Request request, Throwable exception) {
                }
            });
        } 
        catch (RequestException e)  {
        }
    }

我的 API 代码:

    @RequestMapping(value = "/url",
                    method = RequestMethod.POST)
    public @ResponseBody ResponseEntity<?> processPayment(
                                            @RequestParam("request") String payRequestJson,
                                            HttpServletRequest servletRequest) {

        PutUnAccountedTransaction unAccountedTransaction = null ;
        try {
            LOGGER.info("request recieved unaccounted transaction : " +
                    payRequestJson);
             unAccountedTransaction = objectMapper.readValue(payRequestJson, PutUnAccountedTransaction.class) ;
       } catch (Exception e) {
          //currenlty handling success case only.
            return ResponseEntity.ok(unAccountedTransaction);
       }
    return ResponseEntity.ok(unAccountedTransaction);
    }

我在 postman 中调用的 API 具有以下值: 标题:

Content-Type : application/x-www-form-urlencoded

请求:{"applicationIdentiferICC":"jh2b32vy","amount":"100","authCode":"hhbjhbjh","orderNumber":"jhsbhjb","dateTimeInUTC":"03/01/2017 07:20:57","gatewayName":"gatewayname","transactionReference":"asdhgwvg","dealerId":"bhjhahdbj","cardType":"VISA","terminalId":"jjhbjhbhjbj","userReference":"bjhbjh","paymentType":"EMV_CHIP","expMonth":"09","expYear":"19","cardNumber":"1111"}

我得到了预期的回复。

我认为问题是由于“Accept”标头造成的。我已经尝试使用“/”、“application/x-www-form-urlencoded”,但我得到了同样的错误。

我在邮递员中得到的回复:

{
  "gatewayName": "credit-call",
  "dealerId": 1278,
  "terminalId": "jjhbjhbhjbj",
  "amount": 100,
  "orderNumber": "jhsbhjb",
  "cardNumber": "9111",
  "cardType": "Disc",
  "dateTimeInUTC": "03/01/2017 07:20:57",
  "paymentType": "EMV_CHIP",
  "authCode": "hhbjhbjh",
  "transactionReference": "asdhgwvg",
  "userReference": "bjhbjh",
  "applicationIdentiferICC": "jh2b32vy",
  "expMonth": 9,
  "expYear": 12,
  "dateTimeInPST": null,
  "inoiceGuid": null,
  "description": null
}

在浏览器中检查客户端后收到响应头。

Server: Apache-Coyote/1.1
X-Application-Context: postpayment:devvm
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 01 Mar 2017 15:28:17 GMT

我使用的是 GWT 2.5 版本。

我收到以下错误:

java.lang.IllegalArgumentException: empty argument
    at com.google.gwt.json.client.JSONParser.parse(JSONParser.java:210)
    at com.google.gwt.json.client.JSONParser.parseStrict(JSONParser.java:87)

一行

 JSONValue jsonValue = JSONParser.parseStrict(response.getText())

【问题讨论】:

    标签: java json gwt spring-boot gwt-2.5


    【解决方案1】:

    检查您的响应 JSON 并确认它是有效的。该错误表明请求很好,但是当响应 JSON 字符串到达​​浏览器时,JSONParser.parseStrict(response.getText()) 无法解析它,因为它不是合法的 JSON。

    如果您需要更多帮助,请在此处分享响应 JSON 和响应标头。

    【讨论】:

    • 一目了然...你能添加 exact 错误信息吗?它看起来不太正确,但有堆栈跟踪和确切的错误可能会有所帮助。
    • 添加了我遇到的错误以及我遇到该错误的行。
    • 错误信息表示不存在 JSON - 您确定 response.getText() 是您在上面发布的 JSON 吗?因为 parseStrict 不会用非空字符串抛出异常......
    • response.getText() 是空的。我发布的响应是我收到邮递员。我得到 response.getText() 为空。但我不理解 curl 和邮递员工作正常。
    • 好的,这就是你问题的根源。在您的问题中需要更多详细信息才能回答这个问题,但至少从调试器中浏览器的控制台和网络选项卡开始,看看在拨打电话时是否有错误,以及发送了哪些标头以查看是否你做错了什么。但是,是的,空字符串不是有效的 json。
    猜你喜欢
    • 2018-10-02
    • 2017-06-24
    • 2018-02-19
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    相关资源
    最近更新 更多