SpringMVC中使用ajax请求controller后无法执行success回调函数

1、今天上午写了一个简单的demo,用来验证springMVC的功能,前台实现如下:

$.ajax({
		type : 'post',
		url : webAppPath + "scl/pictureData/cmdRequestPictureData.do",
		data : {
			iedName : 'IL114'
		},
		dataType: "json",
		async : false,
		success : function(data) {
			debugger
		}
	});

请求的后台controller如下

@Controller
@RequestMapping(value = "/pictureData")
public class PictureDataRequestController {

	@Resource(name = "sclPictureServiceImpl")
	private PictureDataRequestService pictureDataRequestService;

	@RequestMapping("cmdRequestPictureData.do")
	public String requestPictureData(@RequestParam(value = "iedName", required = false) String iedName) {
		InvokeResult result = new InvokeResult();
		try {
			SCLConst.curIEDName = iedName;
//			Picture picture = pictureDataRequestService.getPictureData();
			result.setSuccessful(true);
//			result.setResultValue(picture);
		} catch (Exception e) {
			result.setSuccessful(false);
			result.setResultHint(e.getMessage());
		}
		String json = result.toJson();
		System.out.println(json);
		return json;
	}
}
希望后台返回一个json格式的字符串,前台在封装为json对象调用

但是,这个死活就进不来success,找了一上午原因也未果;

尝试把前台的dataType改为text后,虽然可以返回到success里面,但是返回的data是一个错误的html文档,于是郁闷了一上午?

我后天封装的json格式也是标准的,为什么就是不进呢?

偶然看到一个注解:@ResponseBody

百度后才发现,这个注解的功能就是把根据前台设置的dataType,去调用相应的HttpMessageConverter,例如json和xml

于是:

修改后台controller代码:

@Controller
@RequestMapping(value = "/pictureData")
public class PictureDataRequestController {

	@Resource(name = "sclPictureServiceImpl")
	private PictureDataRequestService pictureDataRequestService;

	@RequestMapping("cmdRequestPictureData.do")
	@ResponseBody
	public String requestPictureData(@RequestParam(value = "iedName", required = false) String iedName) {
		InvokeResult result = new InvokeResult();
		try {
			SCLConst.curIEDName = iedName;
//			Picture picture = pictureDataRequestService.getPictureData();
			result.setSuccessful(true);
//			result.setResultValue(picture);
		} catch (Exception e) {
			result.setSuccessful(false);
			result.setResultHint(e.getMessage());
		}
		String json = result.toJson();
		System.out.println(json);
		return json;
	}
}
才正常了。


你可能感兴趣的:(错误异常总结)