Home  >  Q&A  >  body text

java - ajax successfully reaches the background, but I don’t know why it keeps calling back the failed function

function a() {
        $.ajax({
        url : "http://localhost:8080/ubi/checkIntegral",
        async : true,
        data:{"carOwnerID":"111111"},
        dataType : 'json',
        type : 'GET',
        success : function() {
            alert("ss");
        },
        error : function(map){
            alert("FALSE");
        }
    });
}

@RequestMapping(value="/checkIntegral",method = RequestMethod.GET)
@ResponseBody
public  Map<String,Long> checkIntegral(@RequestParam  String carOwnerID ,HttpServletRequest request,HttpServletResponse response){
    Long integral = impl.checkIntegral(Long.valueOf(carOwnerID));
    Map<String,Long> map = new HashMap<String, Long>();
    map.put("msg", integral);
    return map;
}
漂亮男人漂亮男人2712 days ago612

reply all(7)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-17 10:08:19

    If the request is successful and data is returned, it may be related to the incorrect format of your returned data. Because you set the dataType : 'json' 预期服务器返回的数据类型。这样往往会进入 error callback. Please exclude the returned data.

    Moreover, errorhas three callback parameters, please print them out yourself.

    Some reasons why ajax jumps into error

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-17 10:08:19

    Pop up your return value and see the data

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-17 10:08:19

    HttpServletResponse conflicts with the ajax callback, just remove HttpServletResponse.

    reply
    0
  • 迷茫

    迷茫2017-05-17 10:08:19

    I see that your dataType : 'json', requires the server to return json format.
    If the data returned by the server is not in json format, it will go into a failed callback.

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-17 10:08:19

    Configure your AJAX dataType: "text", and then use alert(data) to view the return value

    Since Ajax requests are different from responses, the page does not need to be rendered after getting the data, so there is no need for RESPONSE to jump to a new page. So there is no need to RETURN, but print to the requested page through PrintWriter
    @RequestMapping(value="/checkIntegral", method = RequestMethod.GET)
    @ResponseBody
    public void checkIntegral(@RequestParam String carOwnerID,HttpServletRequest request,HttpServletResponse response) {

    Long integral = impl.checkIntegral(Long.valueOf(carOwnerID));
        PrintWriter writer=response.getWriter();
        writer.write(String.valueOf(integral));
        writer.flush();
        writer.close();

    }

    reply
    0
  • ringa_lee

    ringa_lee2017-05-17 10:08:19

    I didn’t notice that this ajax is a cross-domain request.

    reply
    0
  • 怪我咯

    怪我咯2017-05-17 10:08:19

    The data type of your return value is json, but you returned a Map to him in the background. Convert your map to json

    reply
    0
  • Cancelreply