如何缩短jQueryAjax请求时间?

我注意到jQueryAjax请求有时需要1-2秒才能响应,这是正常的吗?有没有办法将时间缩短到最小呢?
响应类型是json,它是一个很小的html代码。
多谢!

Depending on how you coded your solution, the speed largely depends on internet connection from you to server. You can try launch it on localhost to see if it takes that long, if it does, then something is wrong with your code. If you could post a simplified code illustrating the problem we can try to figure it out :D

Here are the places the slowness could be:

  1. Database <--> Web server latency
  2. Database Query
  3. Database <--> Web server bandwidth
  4. Server side script code
  5. Web server <--> client latency
  6. Web server <--> client bandwidth

In short, yes, a delay is normal after the request is sent and before the response comes. However, you didn't specify exactly what is happening with the request.

Is it just the request that is taking up to much time? Is the delay before it sends the request? I work with jQuery's ajax engine all the time in our app. I haven't really ever seen latency before the request is sent. What do you use to monitor the ajax request? You can check this using firebug. Enable the net panel and watch the timing.

If the only delay is after the response comes then it may be the HTML that is returned. Is there a lot of HTML that is returned? Is there javascript in the HTML? Are you injecting the HTML in the page and the delay is after that?