我使用没有Spring Security的Spring MVC ,我遇到了一个问题,因为 HTTP状态405 - 请求方法' POST'不支持 我试过
第一次Ajax通话
$('#id-posts-div').on('click', '.class-add-comment-button', function(){
var $this = $(this);
var comment = $('#id-comment-content-textarea-'+$this.data('id') ).val().trim();
if( comment != '' )
$.ajax({
url: "${pageContext.request.contextPath}/master/addComment?nsec=${nsec}&sesAccess=4",
type: 'POST',
data: JSON.stringify({
postId: $this.data('id'),
content: comment,
isActive: true,
}),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
}).done( function ( comment ) {
alert( JSON.stringify( comment ) );
}).fail( function( jqXHR, textStatus, errorThrown ) {
alert( textStatus+" : "+errorThrown );
});
});
使用以下控制器方法。
@RequestMapping( name="/addComment", method=RequestMethod.POST)
@ResponseBody
public CommentDetails addComment( @RequestBody CommentDetails commentDetails ) {
System.out.println("MasterController.addComment()");
System.out.println( commentDetails.toString() );
return commentDetails;
}
第二次调用Ajax
$('#id-posts-div').on('submit', 'form.class-comment-form', function ( event ) {
event.preventDefault();
var $this = $(this);
var formData = new FormData( this );
var comment = $('textarea', $this).val().trim();
alert( $this.prop("action") );
if( comment != '' )
$.ajax({
url: $this.prop("action"),
type: 'POST',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
}).done( function ( comment ) {
alert( JSON.stringify( comment ) );
}).fail( function( jqXHR, textStatus, errorThrown ) {
alert( textStatus+" : "+errorThrown );
});
});
使用以下控制器方法。
@RequestMapping( name="/addComment", method=RequestMethod.POST)
@ResponseBody
public CommentDetails addComment( @ModelAttribute CommentDetails commentDetails ) {
System.out.println("MasterController.addComment()");
System.out.println( commentDetails.toString() );
return commentDetails;
}
我的表单是动态生成的
可以说,我想发布一些内容(使用ajax),在帖子提交后我已经生成了<HTML>
评论帖子。我尝试了 <form>
和没有 <form>
,如下所示(我删除了装饰<divs>
)
$('#id-posts-div').prepend( '<div class="class-post-div">'+post.content
+'<div class="class-add-cancel-comment-div-'+post.id+'"><a href="#" id="id-add-comment-a-'+post.id+'" class="class-add-cancel-comment" data-id="'+post.id+'">Add Comment</a></div>'
+'<div class="class-add-cancel-comment-div-'+post.id+'" style="display:none;">'
// +'<form method="post" action="${pageContext.request.contextPath}/master/addComment?nsec=${nsec}&sesAccess=4" class="class-comment-form" data-id="'+post.id+'">'
// +'<input type="hidden" value="'+post.id+'" name="postId">'
+'<textarea name="content" id="id-comment-content-textarea-'+post.id+'" class="class-textarea" '
+'placeholder="Add comment on this post?"></textarea>'
+'<button id="id-add-comment-button-'+post.id+'" class="class-add-comment-button class-blue-button" data-id="'+post.id+'">Add Comment</button><br><br>'
// +'<input type="submit" id="id-submit-btn" value="Add Comment"><br><br>'
+'<button id="id-cancel-comment-button-'+post.id+'" class="class-add-cancel-comment class-blue-button" data-id="'+post.id+'">Cancel</button>'
// +'</form>'
+'</div>' );