parsererror:位置0的JSON中的意外标记r

问题描述:

我有一个spring mvc应用程序。我尝试使用下面的方法来发送一个HTTP POST请求:parsererror:位置0的JSON中的意外标记r

$.ajax({ 
     type: 'POST', 
     url: '/suggested-events/vote/'+votedEventId, 
     success: function(response) { console.log("Hey: "+JSON.parse(response)); }, 

     error: function(XMLHttpRequest, textStatus, errorThrown) { 
      console.log("TextStatus: "+textStatus); 
      console.log("ErrorThrown: "+errorThrown); 
     }, 
     contentType: "application/json", 
     dataType: 'json' 
    }); 

HTTP POST请求成功触发低于其他控制器的方法:

@PostMapping("/suggested-events/vote/{votedEventId}") 
    public ResponseEntity<String> voteForASuggestedEvent(@PathVariable Long votedEventId){ 
     log.info("You have made a vote for: "+votedEventId); 
       HttpHeaders responseHeaders = new HttpHeaders(); 

    return new ResponseEntity<String>("responseHeaders", responseHeaders, HttpStatus.CREATED); 

    } 

但正如你看到的,我有兴趣接收,并在我的控制台打印响应数据:

success: function(data) { console.log("Hey: "+data); } 

的问题是:什么也没有发生在我的控制台

我确定BackEnd收到请求并提供答案。

下面的图像显示了REST响应到达网络中的浏览器控制台检查

enter image description here

但它不会印在控制台

+0

所以添加一些错误处理,也要检查浏览器开发工具网络的实际要求 – charlietfl

+0

是的。添加错误处理程序并检查一个jax请求 – charlietfl

jQuery的AJAX自动分析数据,如果的contentType是设置为应用程序/ json,因此您不需要在此处解析

console.log("Hey: "+JSON.parse(response)); 

替换与

console.log("Hey: "+ response); 

除此之外,如果你指望JSON,你需要从你的控制器返回JSON ...

+0

仍抱怨有相同的错误 –