java - ajax successfully reaches the background, but I don't know why it keeps calling back the failed function
漂亮男人
漂亮男人 2017-05-17 10:06:19
0
7
659
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;
}
漂亮男人
漂亮男人

reply all(7)
曾经蜡笔没有小新

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

曾经蜡笔没有小新

Pop up your return value and see the data

巴扎黑

HttpServletResponse conflicts with the ajax callback, just remove HttpServletResponse.

迷茫

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.

淡淡烟草味

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();

}

左手右手慢动作

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

刘奇

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template