相同的觀點我托盤數據保存到服務器,並刷新查看與添加的數據。將多部分請求發送到服務器之後。在服務器部分,可以將數據保存到base和back json對象。問題是,無法刷新相同的視圖,服務器通過操作方法發送json響應。我可以重定向,但這個不行。沒有錯誤出現。我發現simile後,它呼籲使用e.preventDefault()觸發click()函數和Ajax選項覆蓋:true,但這沒有幫助。如何bypath呢?回到阿賈克斯後請求和響應春季
Template code .
<form action="/video/addCasePost" id="formAddPoster" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="caseTitle" class="col-sm-2 control-label">title</label>
<div class="col-sm-10">
<input type="text" class="form-control input-sm" id="caseTitle" name="caseTitle" value="" placeholder=「add」 title here … />
</div>
</div>
<div class="form-group">
<label for="casePosterLoad" class="col-sm-2 control-label">post</label>
<div class="col-sm-10 ">
<input type="file" name="file" required id="fileLoader" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" class="form-control input-sm" id="postId" name="postId" value="{{item.postId}}" />
<button type="submit" id="btnSaveCasePost" class="btn btn-default"></button>
<!-- save content -->
</div>
</div>
function casePostUpload()
{
var myForm = form.find('formAddPoster');
myData = new FormData();
myData.append('multipart', myForm);
$
.ajax({
url : "/video/addCasePost",
data : myForm,
type : 'POST',
headers: {'Content-Type': undefined},
enctype: 'multipart/form-data',
processData: false,
contentType:false,
success : function(data, textStatus, jqXHR){
$('#casePost').prepend('<br/>Poster: '+data.casePoster+' :: title '+data.caseTitle+' at '+data.timestampAsStr+'<br/></td></tr>');
},
error : function(result){
//...;
alert('add contnt error: ' + errorThrown);
}
});
}
Spring MVC controller method
@RequestMapping(value = "/video/addCasePost", method = RequestMethod.POST)
public @ResponseBody CasePost addcasepost(@RequestPart("caseTitle") String caseTitleString,
@RequestPart("file") MultipartFile file, HttpServletRequest request, Model model, @ModelAttribute("currentuser") User currentuser) throws Exception {
CasePost jsResponse = new CasePost();
String filePath = new String();
//save file to directory
if (!file.isEmpty()) {
try {
String uploadsDir = 「/fd/ct/images/";
String realPathtoUploads = request.getServletContext().getRealPath(uploadsDir);
if(! new File(realPathtoUploads).exists())
{
new File(realPathtoUploads).mkdir();
}
String orgName = file.getOriginalFilename();
filePath = realPathtoUploads + orgName;
File dest = new File(filePath);
file.transferTo(dest);
}catch (Exception e) {
log.error(e);
}
}
try {
if (caseTitleString != null) {
jsResponse.setCaseTitle(caseTitleString);
jsResponse.setCasePoster(filePath);
jsResponse = graphStoryInterface.getCasePostInterface().add(jsResponse, currentuser);
}
}
catch (Exception e) {
log.error(e);
}
return jsResponse;
}
通過「刷新視圖」,你的意思是添加一些HTML到現有的元素?你的ajax調用顯示你在'#casePost'元素中添加了一些東西,但是這似乎並不存在於你的HTML代碼中。 – dhc
你要麼使用表單動作,要麼你單獨使用ajax。由於您使用的是Ajax,因此您可能會擺脫formAction,然後處理響應中的數據。無需重新加載/重定向。 –
冬季戰士對於發送請求只使用ajax。 – abmerday