4

我有这样的ajax json POST方法

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

处理发布请求的控制器

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 无法加载 localhost:8080/webeditor/spring/json/。仅 HTTP 支持跨源请求。

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

4

6 回答 6

5

我的最终作品版本

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 );
}
于 2012-07-06T18:38:08.380 回答
2

使用 Spring 传递 JSON 相当简单。考虑以下 jQuery 函数:

function processUrlData(data, callback) {
    $.ajax({
        type: "GET",
        url: "getCannedMessageAsJson.html",
        data: data,
        dataType: "json",
        success: function(responseData, textStatus) {
            processResponse(responseData, callback);
        },
        error : function(responseData) {
            consoleDebug("  in ajax, error: " + responseData.responseText); 
        }
    });
}

现在使用以下 String @Controller 方法...

@RequestMapping(value = "/getCannedMessageAsJson.html", method = RequestMethod.POST) 
public ResponseEntity<String> getCannedMessageAsJson(String network, String status, Model model) {

    int messageId = service.getIpoeCannedMessageId(network, status);
    String message = service.getIpoeCannedMessage(network, status);

    message = message.replaceAll("\"", "&quot;");
    message = message.replaceAll("\n", "");

    String json = "{\"messageId\": \"" + messageId 
    + "\", \"message\": \"" + message + "\"}"; 

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_JSON);
    return new ResponseEntity<String>(json, responseHeaders, HttpStatus.CREATED);
}

在我的情况下,请求非常简单,我只是在控制器方法中硬连线 json 格式,但您可以轻松地使用像 Jackson 这样的库来生成 json 字符串。

也正如其他人所说,验证 @RequestMapping 中的“值”是一个唯一的、合法的文件名。使用我上面展示的 json 方法,您不必拥有相应的 jsp 页面(实际上它不会使用)。

于 2012-07-06T15:40:12.183 回答
0

您的应用程序应该有一个上下文根,它将位于 URL 路径的其余部分之前。你还应该servlet-mapping在 web.xml 中有一个定义,它定义了哪些请求被定向到你的 Spring 控制器。因此,如果您的应用程序的上下文根是“myapp”并且您servlet-mapping将转到 *.html,那么您的 ajax 调用将如下所示:

$.ajax({
    type: 'POST',
    url: "/myapp/webeditor/spring/json.html",
    data: JSON.stringify(contents),
    dataType: "json",
    success: function(response) {
        // Success Action
    }
}); 
于 2012-07-06T15:01:00.943 回答
0

在 URL 中: url: "localhost:8080/webeditor/spring/json/"

webeditor 必须是战争名称或服务名称,所以在你的 @RequestMapping(value="/webeditor/spring/json/" 我认为你不应该有 'webeditor' 它必须只有/spring/json

通常 404 表示 URL 请求错误或没有为该 URL 运行此类服务

于 2012-07-06T13:40:09.820 回答
0

看起来像 jQuery 所以为什么不试试

$.getJSON('webeditor/spring/json', JSON.stringify(contents, function(data) {//do callbackstuff});

如果您想请求跨域,方法如下:-

cbFn = function(data) {
   // do callback stuff. 
}

    var ca = document.createElement('script');
                ca.type = 'text/javascript';
                ca.async = true;
                ca.src = server + '/webeditor/spring/json.jsonp?callback=cbFn';
                var s = document.getElementsByTagName('head')[0];
                s.parentNode.insertBefore(ca, s);

并添加 servlet 映射

<servlet-mapping>
    <servlet-name>yourSevletName</servlet-name>
    <url-pattern>*.jsonp</url-pattern>
</servlet-mapping>
于 2012-07-06T16:35:32.240 回答
-1

在你的 jsp 中包含这样的标签库

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

然后使用spring创建一个完整的url

<c:url var="yourFullUrl" value="/webeditor/spring/json/" />

然后基于此创建 javascript 变量,以便您可以在 Ajax 中使用

<script>
var yourUrl= '<c:out value="${yourFullUrl}"/>';
</script>

不使用表示 url 的 javascript 变量:

<script>
$.ajax({
        type: 'POST',
        url: yourUrl, 
        data: JSON.stringify(contents),
        dataType: "json"
});
</script>
于 2012-07-06T12:42:07.090 回答