我是jQuery的新手。我想更新视图而不刷新页面。数据正在控制台中登录,但没有刷新就无法加载到View中。 $('#ajax')。html(ajax); -什么也没给我看。我叫数据AJAX。
JavaScript
<script>
$(document).ready(function() {
$('#api_key').click(function() {
var request = $.ajax({
url: "/account/settings/api_token",
method: "POST",
});
request.done(function(msg) {
if (msg['status'] == 'success') {
var request2 = $.ajax({
url: "/account/settings/get_all_tokens",
method: "GET",
});
request2.done(function(ajax) {
$('#ajax').html(ajax);
console.log(ajax);
});
request2.fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}
});
request.fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
});
});
</script>
HTML
<div class="api-cta-container">
<p> <%= t("account.settings.api.disclaimer_text_html") %></p>
<button type="button" id="api_key" class="btn btn-default"><%=
t("account.settings.api.add_api_key") %></button>
<%#= link_to t("account.settings.api.add_api_key"),
/account/settings/api_tokens, method: :post, class: "btn btn-default" %>
</div>
<table class="table" id="ajax">
<thead>
<tr>
<th><%= t("global.name") %> <</th>
<th><%= t("global.creation_date") %></th>
</tr>
</thead>
<tbody>
<% @all_tokens.each_with_index do |token, index|%>
<tr>
<td><%= (index+1) %></td>
<td><%= token.created_at %></td>
<button type="button" class="btn btn-default"><%= t("global.edit") %> <i class="fa fa-sort-desc" aria-hidden="true"><i></button>
</td>
</tr>
<% end %>
</tbody>
控制台中的数据:成功后,这是对象中的数据
(4) [{…}, {…}, {…}, {…}]
0:{granted_by: "service", created_at: "2018-07-23T01:06:12.716-04:00",
time_to_live: 1532927173, refresh_token_expires_at: "2018-07-
30T01:06:12.714-04:00", client_id: "0f28ad93-d481-47f5-aaec-
5b9d9b1edff9", …}
1:{granted_by: "service", created_at: "2018-07-23T00:37:29.244-04:00",
time_to_live: 1532925449, refresh_token_expires_at: "2018-07-
30T00:37:29.243-04:00", client_id: "0f28ad93-d481-47f5-aaec-
5b9d9b1edff9", …}
2:{}
3:{} .........
答案 0 :(得分:0)
$(document).ready(function() {
$.ajax({
url: "/account/settings/api_token",
method: "POST",
}).done(function(msg) {
if (msg['status'] == 'success') {
$.ajax({
url: "/account/settings/get_all_tokens",
method: "GET",
}).done(function(ajax) {
$('#ajax').html(ajax);
console.log(ajax);
}).fail(function(jqXHR, textStatus) {
console.log("Request2 failed: " + textStatus);
});
}
}).fail(function(jqXHR, textStatus) {
console.log("Request1 failed: " + textStatus);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
尝试一下。如果有帮助。或使用jQuery.When