Hi everyone!
I have script in Spring MVC application which adds an entry in the table.
$(document).ready(function () {
$('#saveSubject').submit(function (e) {
$.post('/university/subjectAdd', $(this).serialize(), function (subject) {
$('#subjectsTableResponse').last().append(
'<tr>' +
'<td align=\"center\">' + subject.title + '</td>' +
'<td align=\"center\">' + '<a href=\"c:url value=\'/subject/update/{'+subject.id+'}\'/>' + Update + '</a>'+'</td>'+
'<td align=\"center\">' + '<a href=\"c:url value=\'/subject/delete/{'+subject.id+'}\'/>' + Delete + '</a>'+'</td>'+
'</tr>'
);
});
clearInputs();
e.preventDefault();
});
});
But when you add the recording error takes related link
Uncaught ReferenceError: Update is not defined
My table:
<div class="tableSubjects">
<table border=2 bgcolor="#C1CDCD" id="subjectsTableResponse">
<tr>
<td align="center"><B>Предмет</B></td>
</tr>
<c:forEach items="${subjectList}" var="subject">
<c:if test="${subject.deleted eq false}">
<tr>
<td align="center">${subject.title}</td>
<td align="center">
<a href="<c:url value='/subject/update/${subject.id}' />">Update</a>
</td>
<td align="center">
<a href="<c:url value='/subject/delete/${subject.id}' />">Delete</a>
</td>
</tr>
</c:if>
</c:forEach>
</table>
</div>
enter code here
how to fix this error?
You have two issues:
If Update
and Delete
are supposed to be literal strings, they should be enclosed in quotes.
In order to have subject
passed as an object rather than a string of JSON, you have to pass a data type of 'json'
to $.post
.
$(document).ready(function () {
$('#saveSubject').submit(function (e) {
$.post('/university/subjectAdd', $(this).serialize(), function (subject) {
$('#subjectsTableResponse').last().append(
'<tr>' +
'<td align=\"center\">' + subject.title + '</td>' +
'<td align=\"center\">' + '<a href=\"c:url value=\'/subject/update/{'+subject.id+'}\'/>Update</a>'+'</td>'+
'<td align=\"center\">' + '<a href=\"c:url value=\'/subject/delete/{'+subject.id+'}\'/>Delete</a>'+'</td>'+
'</tr>'
);
}, 'json');
clearInputs();
e.preventDefault();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div>