0

I want to make an AJAX call to a file on a server, from jQuery ...

The problem is that this file is generated when I do a post from another pc / browser.

What I want is that the request still listening, until the file is generated, and do not return a 404 response...

any ideas?

3 Answers 3

2

You can use setTimeout to repeatedly check, if the response has been 200, positive. You can adjust the timeout value to balance the load on the server and the UI experience. Also, if everything fails, a counter could limit the number of total tries.

3
  • but the server will receive a lot of innecesary requests :/
    – Pablo
    May 20, 2011 at 0:03
  • Not necessarily; if e.g. on average a file is ready for download in 0.85s, then you can let the javascript get the file after one second, which in most cases should result in a STATUS_OK, 200. If something goes wrong, you could increase the timeout by a factor of two: the js would ask for the file after 1s, 2s, 4s, 8s, etc. up to a maximum. This way you would only request each file for at most 5 times, if we say the maximum would be 30s.
    – miku
    May 20, 2011 at 0:08
  • mmm... yep, but i mean i could take 3 hours maybe... i wanna to open a port and let the server send a notification to the client when could find the file, and just make one correctly request... but i dont know how to do it on Ruby on Rails :/
    – Pablo
    May 20, 2011 at 0:14
0

Write a server-side script which will wait until the file exists, then return its contents. (or redirect to the file)

5
  • I think the solution could be a socket in the server... but... how to make a socket in ruby, i dont find anything in google :/
    – Pablo
    May 19, 2011 at 22:25
  • @Pablo: Huh? What are you talking about?
    – SLaks
    May 19, 2011 at 22:47
  • i dont find a "nice" way to wait until the file exists, i think about a notification from the server to the client...
    – Pablo
    May 19, 2011 at 23:45
  • I mean, I think that the best way, is that the server tell the client when to find the file, to an open channel... its not a socket??
    – Pablo
    May 20, 2011 at 0:04
  • You can't do that on the client (except for HTML5 WebSockets). You can just use a single long-running request.
    – SLaks
    May 20, 2011 at 0:28
0

I don't think you can do exactly what you're asking. Best bet would probably be to put the Ajax call in a while loop to just poll the server until it returns the file.

2
  • 1
    A while loop won't help. You need to use the callback.
    – SLaks
    May 19, 2011 at 1:06
  • with a while it works, but i dont wanna make thousands of request :/
    – Pablo
    May 19, 2011 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.