我正在尝试从AJAX调用中呈现ModelAndView。点击进入控制器并返回ModelAndView。但是,当我尝试通过$(#div).html(响应)渲染该对象时,它只显示一个空白页面。我使用$(#div).text(响应)检查我是否收到HTML响应,而且我这样做。我收到的HTML响应是整个页面,而不仅仅是我在ModelAndView中返回的DIV片段。我完全困惑,因为我在不同的项目中成功地使用了这种方法。以下是代码段:
Javascript文件>>>
$.ajax({
type: "GET",
url: contextPath + "/order/orderMiscDetails/"+orderNo,
success: function(response) {
$("#divPayment").empty();
$('#divPayment').html(response);
//ajaxCallEnd();
}
});
控制器代码>>>>
@RequestMapping(value = "order/orderMiscDetails/{orderNo}",method=RequestMethod.GET)
public ModelAndView orderMiscDetails(@PathVariable("orderNo") String orderNoStr,
Model model) throws Exception {
Long orderNo = 0l;
System.out.println("Called>>>>>>>>>>>>>>orderMiscDetails>>>>>>>>");
CustomerOrder customerOrder = new CustomerOrder();
customerOrder.setOrderNumber(orderNo);
customerOrder = orderAdminService
.getCustomerOrderByOrderNo(customerOrder);
Map<String,Object> modelMap = new HashMap<String, Object>();
modelMap.put("customerOrder", customerOrder);
ModelAndView mv= new ModelAndView("order/payment_history",modelMap);
System.out.println(mv.toString());
return mv;
}
DIV片段JSP&gt;&gt;&gt;&gt;
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<div class="heading">PAYMENT HISTORY</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="30">S.No.</th>
<th>Transaction Date</th>
<th align="right" style="text-align: right;">Transaction
Amount</th>
<th>Transaction Type</th>
<th>Transaction ID</th>
<th>MATransaction ID</th>
<th>Reason</th>
<!-- <th width="100">Action</th>-->
</tr>
<c:forEach items="${customerOrder.orderTransactions}"
var="orderTransaction" varStatus="loopStatus">
<%-- <c:if test="${orderTransaction.orderTransactionStatus eq '0'}"> --%>
<tr id='transaction_${orderTransaction.ordertransactionID}'>
<td>${loopStatus.index+1}</td>
<td>${orderTransaction.date}</td>
<td align="right">
${customerOrder.landing_page_id.currencies.currencies_symbol} ${orderTransaction.transactionAmt}
</td>
<td>${orderTransaction.transactionType}</td>
<td>${orderTransaction.transactionID}</td>
<td><c:if test="${empty orderTransaction.mATransactionID}">N/A</c:if>${orderTransaction.mATransactionID}</td>
<td>${orderTransaction.reason}</td>
<%-- <input type="hidden" id="param1" value="${orderTransaction.ordertransactionID}"/> --%>
<!-- <td><a onClick=deleteOrderTransaction();>Delete</a></td> -->
</tr>
<%-- </c:if> --%>
</c:forEach>
</table>
Spring设置有问题吗?
SOLUTION:
我能够解决这个问题。但是,解决方案非常奇怪,我不知道它是如何工作的!
出于某种原因,jquery的.html()无效。当我更换时它起作用了 $(&#39;#divPayment&#39;)HTML(响应)。 通过 document.getElementById(&#39; divPayment&#39;)。innerHTML = response;
由于我在整个相同的JSP中使用,因此JQuery被正确包含在内。不管是什么,谢谢大家!
答案 0 :(得分:0)
如果你尝试怎么办?
html = $.parseHTML(response);
// first build html structure from the response
$('#divPayment').html(html);
// And after insert this html into the payment div