我正在使用网络服务。我正在拨打 GET 电话以获取所有问题 -> 用户将回答该问题并使用 post 电话发布。这部分现在按预期工作。现在,我想在 POST 调用成功后立即进行另一个 GET 调用。POST 调用完成后,我可以进行 GET 调用,但视图仍显示来自旧 GET 调用的数据。如何使用来自新 GET 调用的信息更新视图。
GET -> POST -> New GET(此调用中未更新数据。)
JSON
{
"Respondent_ID":"hello111",
"Group_Name":"",
"Product_ID":80,
"Language_ID":1,
"First_Name":"hello",
"Last_Name":"111",
"Respondent_EMail":"",
"Gender":"M",
"AllQuestions":[
{
"Question_Number":76,
"Question_Text":"I think ",
"Definition":"",
"Answer":0
},
{
"Question_Number":77,
"Question_Text":"I am ",
"Definition":"",
"Answer":0
},
{
"Question_Number":78,
"Question_Text":"I am mild mannered",
"Definition":"",
"Answer":0
},
{
"Question_Number":79,
"Question_Text":"I am strong",
"Definition":"",
"Answer":0
},
{
"Question_Number":80,
"Question_Text":"I am a risk taker",
"Definition":"",
"Answer":0
}
],
"AnswerChoice":[
{
"Answer_Choice":"Strongly disagree",
"Answer_Choice_Value":1
},
{
"Answer_Choice":"Disagree",
"Answer_Choice_Value":2
},
{
"Answer_Choice":"Neutral",
"Answer_Choice_Value":3
},
{
"Answer_Choice":"Agree",
"Answer_Choice_Value":4
},
{
"Answer_Choice":"Strongly agree",
"Answer_Choice_Value":5
}
]
}
--
@{
ViewBag.Title = "Questions";
}
<html>
<body>
<script src="~/Scripts/knockout.mapping-latest.js"></script>
<script>
function GetAllEmployees() {
$.ajax({
url: '/api/Questions?respondent_id=hello111',
type: 'GET',
dataType: 'json',
success: function (data) {
var data2 = data.AllQuestions;
var viewModel = {
data: ko.mapping.fromJS(data2),
Question_Number: ko.observable(data.AllQuestions[0].Question_Number),
Question_Text: ko.observable(data.AllQuestions[0].Question_Text),
save: function () {
$.ajax({
url: '/api/lms',
type: 'POST',
data: data,
dataType: 'json',
success: function (data) {
$.ajax({
url: '/api/Questions?respondent_id=hello111',
type: 'GET',
dataType: 'json',
success: function (data) {
//How can update the view with the new data I got from the get call.
}
ko.applyBindings(viewModel);
}
});
},
error: function (x, y, z) {
alert(x + '\n' + y + '\n' + z);
}
});
}
}
ko.applyBindings(viewModel);
},
error: function (x, y, z) {
alert(x + '\n' + y + '\n' + z);
}
});
}
</script>
</body>
</html>
<a href="javascript:GetAllEmployees();"> Get Questions</a>
<form data-bind="submit: save">
<table>
<thead>
<tr><th>#</th>Question<th>Strongly disagree</th><th>Strongly disagree</th><th>Disagree</th><th>Neutral</th><th>Agree</th><th>Strongly agree</th></tr>
</thead>
<tbody data-bind="foreach: $data">
<tr>
<td>
<span data-bind="text: Question_Number"></span>
</td>
<td>
<span data-bind="text: Question_Text"></span>
</td>
<td><input type="radio" class="radio" value="1" data-bind="attr: { name: Question_Number}"></td>
<td><input type="radio" class="radio" value="2" data-bind="attr: { name: Question_Number }"></td>
<td><input type="radio" class="radio" value="3" data-bind="attr: { name: Question_Number }"></td>
<td><input type="radio" class="radio" value="4" data-bind="attr: { name: Question_Number }"></td>
<td><input type="radio" class="radio" value="5" data-bind="attr: { name: Question_Number }"></td>
</tr>
</tbody>
</table>
<button type="submit">Go</button>
</form>