发布JSON数据 - jQuery,Ajax,Spring

时间:2017-01-25 18:13:47

标签: javascript jquery ajax spring-mvc

当我尝试发回消息时,我遇到了错误。

messages.jsp

function success(data) {
    $("#form" + data.target).toggle();
    $("#alert" + data.target).text("Message sent.")
    startTimer();

}

function error(data) {
    alert("Error sending message");
} 

function sendMessage(i, name, email){
    var text = $("#textbox" + i).val();

    $.ajax({
        type: "POST", 
        url: '<c:url value="/sendmessage" />',
        data: JSON.stringify({"target": i, "text": text, "name": name, "email": email}),
        success: success,
        error: error,
        contentType: "application/json",
        dataType: "json"
    });

}

function showMessages(data){

    $("div#messages").html("");

    for(var i=0; i<data.messages.length; i++) {
        var message = data.messages[i];

        var messageDiv = document.createElement("div");
        messageDiv.setAttribute("class", "message");

        var subjectSpan = document.createElement("span");
        subjectSpan.setAttribute("class", "subject");
        subjectSpan.appendChild(document.createTextNode(message.subject));

        var contentSpan = document.createElement("span");
        contentSpan.setAttribute("class", "contentText");
        contentSpan.appendChild(document.createTextNode(message.content));

        var nameSpan = document.createElement("span");
        nameSpan.setAttribute("class", "nameSpan");
        nameSpan.appendChild(document.createTextNode("From: "+ message.name + '('));

        var link = document.createElement("a");
        link.setAttribute("class", "replylink");
        link.setAttribute("href", "#");
        link.setAttribute("onClick", "showReply(" + i + ")");
        link.appendChild(document.createTextNode(message.email));
        nameSpan.appendChild(link);
        nameSpan.appendChild(document.createTextNode(")"));

        var alertSpan = document.createElement("span");
        alertSpan.setAttribute("class", "alert");
        alertSpan.setAttribute("id", "alert" + i);          

        var replyForm = document.createElement("form");
        replyForm.setAttribute("class", "replyForm");
        replyForm.setAttribute("id", "form" + i);

        var textarea = document.createElement("textarea");
        textarea.setAttribute("class", "replyArea");
        textarea.setAttribute("id", "textbox" + i);


        var replyButton = document.createElement("input");
        replyButton.setAttribute("class", "replyButton");
        replyButton.setAttribute("type", "button");
        replyButton.setAttribute("value", "reply");
        replyButton.onclick = function(j, name, email) {
            return function() {
                sendMessage(j, name, email);
            }
        }(i, message.name, message.email);

        replyForm.appendChild(textarea);
        replyForm.appendChild(replyButton);

        messageDiv.appendChild(subjectSpan);
        messageDiv.appendChild(contentSpan);
        messageDiv.appendChild(nameSpan);
        messageDiv.appendChild(alertSpan);
        messageDiv.appendChild(replyForm);


        $("div#messages").append(messageDiv);
    }
}

controller.java

@RequestMapping(value="/sendmessage", method=RequestMethod.POST, produces="application/json")
@ResponseBody
public Map<String, Object> sendMessages(Principal principal, @RequestBody Map<String, Object> data)
{

    String text = (String)data.get("text");
    String name = (String)data.get("name");
    String email = (String)data.get("email");
    Integer target = (Integer)data.get("target");

    System.out.println(name + " , " + email + " , " + text);

    Map<String, Object> returnVal = new HashMap<String, Object>();      
    returnVal.put("success", true);
    returnVal.put("target", target);

    return returnVal;
}

我无法发布消息。

我一直得到此错误的任何帮助或原因?

jquery.js:4 POST http://localhost:8080/spring/sendmessage 403 (Forbidden) send @ jquery.js:4 ajax @ jquery.js:4 sendMessage @ messagesView:32 (anonymous function) @ messagesView:90

我知道我应该禁用CSRF保护。但我不知道如何连接此函数和ajax请求。我已经尝试了很多方法来做到这一点,但没有一个不起作用。

$(function () {
    var token = $("meta[name='_csrf']").attr("content");
    var header = $("meta[name='_csrf_header']").attr("content");
    $(document).ajaxSend(function(e, xhr, options) {
        xhr.setRequestHeader(header, token);
    });
});

0 个答案:

没有答案