java - Ajax json POST 和 Spring MVC Controller

标签 java ajax spring spring-mvc

我有这样的 ajax json POST 方法

$.ajax({
    type: 'POST',
    url: "localhost:8080/webeditor/spring/json/", 
    data: JSON.stringify(contents),
    dataType: "json"
});

处理发布请求的 Controller

JSONPObject json;
BindingResult result = new BeanPropertyBindingResult( json , "MyPresentation" );
@RequestMapping(value="json/", method = RequestMethod.POST)
public void savePresentationInJSON(Presentations presentation,BindingResult result) {
        //do some action

}

但是我得到了这个错误

XMLHttpRequest cannot load localhost:8080/webeditor/spring/json/. Cross origin requests are only supported for HTTP.

我不确定如何更正上述错误。

最佳答案

我的最终作品版本

var jsonfile={json:JSON.stringify(contents)};
$.ajax({
    type: 'POST',
    url: "/webeditor/spring/json/", 
    data: jsonfile,
    dataType: "json"
});

AJAX 和

@RequestMapping(value = "/json/", method = RequestMethod.POST)
public void saveNewUsers( @RequestParam ("json") String json)
{
    System.out.println( json );
}

关于java - Ajax json POST 和 Spring MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11361744/

相关文章:

java - Spring Boot - 批量更新单独插入记录而不是批量插入记录

java - 我该如何解决有关 TestNG 和 Spring Boot 的问题

java - 在根目录下创建文件,奇怪的响应

java - 我可以使用在当前编辑器中打开的文件创建工作集吗?

javascript - ajax 加载后 jQuery 停止工作

php - PHP 中默认的 session 过期时间是多少?

javascript - 使用ajax从xml url获取纬度和经度

java - 对子类使用 JPA 2.0 @Inheritance 注释是否有意义?

Java - 模式匹配

java - HTTP 状态 406。Spring MVC 4.0、jQuery、JSON