如何预取一个在浏览器中返回JSON格式数据的ajax调用

时间:2022-10-18 14:06:29

I have a use case wherein I am making multiple ajax calls when the page loads. The initial load fires off an ajax request to the server that gets the data in JSON format, upon subsequent user clicks other ajax calls are fired that make database calls to compute the JSON on the server side , the browser typically is idle and waits for the JSON to be returned. I was thinking of some ways in which I might be able to speed up the execution of this from the application side.

我有一个用例,其中我在页面加载时进行多个ajax调用。初始加载会向获取JSON格式数据的服务器发出ajax请求,在后续用户单击时会触发其他ajax调用,这些调用会使数据库调用在服务器端计算JSON,浏览器通常处于空闲状态并等待要返回的JSON。我正在考虑一些方法,我可以从应用程序方面加快执行此操作。

Is there a way in Jquery to fire off the subsequent ajax request before hand and save it somewhere on the client , so that when a user click happen the page the JSON gets parsed from the cache and rendered in the DOM instead of the delayed request to the server.

在Jquery中是否有一种方法可以手动触发后续的ajax请求并将其保存在客户端的某个位置,这样当用户单击发生该页面时,JSON将从缓存中解析并在DOM中呈现而不是延迟的请求服务器。

2 个解决方案

#1


2  

jQuery AJAX requests are being cached by the browser (assuming it is a GET request, POST will always hit the server). So if you can predict the calls most likely to happen, you can spin off a function during initialization to make these calls preemptively, and you should get the results for the subsequent calls from the browser cache. Make sure the server actually sends appropriate cache headers, though.

jQuery AJAX请求正在被浏览器缓存(假设它是一个GET请求,POST将始终命中服务器)。因此,如果您可以预测最有可能发生的调用,您可以在初始化期间分离一个函数以抢先进行这些调用,并且您应该从浏览器缓存中获取后续调用的结果。但请确保服务器实际发送了适当的缓存标头。

Another option would be to wrap the call to the jQuery AJAX function in a function of your own and hold the data in an array in memory. In this case, make sure you have a way of aging out old results at some point, or you will fill up the browsers memory.

另一个选择是在你自己的函数中包装对jQuery AJAX函数的调用,并将数据保存在内存中的数组中。在这种情况下,请确保您在某些时候有办法老化旧结果,否则您将填满浏览器内存。

#2


0  

What you can do is fire off all the AJAX requests when you load the page, and assign them to promise variables. When you need to use the result, use $.when()

您可以做的是在加载页面时触发所有AJAX请求,并将它们分配给promise变量。当您需要使用结果时,请使用$ .when()

var ajax1 = $.ajax( { ... } );
...
$.when(ajax1).then(function(response) {
    // Do what you need
});

You can

#1


2  

jQuery AJAX requests are being cached by the browser (assuming it is a GET request, POST will always hit the server). So if you can predict the calls most likely to happen, you can spin off a function during initialization to make these calls preemptively, and you should get the results for the subsequent calls from the browser cache. Make sure the server actually sends appropriate cache headers, though.

jQuery AJAX请求正在被浏览器缓存(假设它是一个GET请求,POST将始终命中服务器)。因此,如果您可以预测最有可能发生的调用,您可以在初始化期间分离一个函数以抢先进行这些调用,并且您应该从浏览器缓存中获取后续调用的结果。但请确保服务器实际发送了适当的缓存标头。

Another option would be to wrap the call to the jQuery AJAX function in a function of your own and hold the data in an array in memory. In this case, make sure you have a way of aging out old results at some point, or you will fill up the browsers memory.

另一个选择是在你自己的函数中包装对jQuery AJAX函数的调用,并将数据保存在内存中的数组中。在这种情况下,请确保您在某些时候有办法老化旧结果,否则您将填满浏览器内存。

#2


0  

What you can do is fire off all the AJAX requests when you load the page, and assign them to promise variables. When you need to use the result, use $.when()

您可以做的是在加载页面时触发所有AJAX请求,并将它们分配给promise变量。当您需要使用结果时,请使用$ .when()

var ajax1 = $.ajax( { ... } );
...
$.when(ajax1).then(function(response) {
    // Do what you need
});

You can