使用 JQuery 进行跨域请求
这是此处问题的后续问题,
这里简要介绍一下我正在努力做。文件服务器创建一个文本文件来指示进程的结束。在 Web 服务器上的网页上,我每 x 秒循环一次并发出 ajax 请求以查明测试文件是否存在(对 http://fileserver/files/UserFile.txt
的 ajax 请求
)到目前为止,已经尝试了以下方法:
- 从客户端触发一个 Web 方法,该方法创建一个 HttpContext 对象来验证文本文件是否存在。但这对服务器来说太费力了,我开始在事件查看器中收到各种异常。
- YQL 效果很好,但不幸的是它太慢了。用户等待的时间至少是原来的两倍。
我正在寻找一种不涉及服务器端的解决方案。不知何故,我想使用 JQuery 来验证文件服务器上文本文件的存在。
有什么想法吗?
This is a followup question to the one here
Here's briefly what I am trying to do. The File server creates a text file to indicate an end of the process. On a webpage on the Web Server, I loop every x seconds and make an ajax request to find out if the test file exists (ajax request to http://fileserver/files/UserFile.txt
)
I've tried the following approaches so far:
- Trigger a web method from the client side that creates a HttpContext object to verify if the text file exists. But this is too strenous on the server and I started getting all kinds of exceptions in the Event Viewer.
- YQL works great but unfortunately it's too slow. The user waits atleast twice the amount of time.
I am looking for a solution that doesn't involve the server side. Somehow, I'd like to use JQuery to verify the existence of a text file on the fileserver.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能离基地很远,但是……你能不能创建一个具有巨大超时的异步(!)Ajax 客户端请求。开火,然后等待。您将调用一些服务器脚本,在服务器上的循环中(在其间使用睡眠)经常检查该文件是否存在。并且在文件最终显示之前不回复 Ajax 请求。然后服务器脚本回复并退出。
编辑:根据所使用的服务器端脚本框架,您甚至可能获得一些操作系统支持。您也许可以在目录中的状态更改时休眠...
I may be miles off base here but... could you not create ONE asynchronous (!) Ajax client request with a HUMONGOUS timeout. Fire it, and wait. You would be invoking some server script that checks every so often, in a loop on the server (using sleep in between), whether the file exists. And not replying to the Ajax request until the file finally shows. The server script then replies and exits.
EDIT: Depending on the server-side scripting framework used, you may even get some OS support. You may be able to sleep on a status change in the directory...
从文件系统提供单个文件是 Web 服务器可以执行的最简单的操作。如果这已经太多了,那么所有其他解决方案都会更糟糕。找出为什么服务器需要这么长时间来提供一个简单的文件并修复它。
注意:我假设该文件很小,因为您说“测试文件”。如果它是一个大文件,服务器实际上会将其发送到客户端,这将需要大量资源。
您可以尝试向网站添加一个 ASP 页面,该页面在服务器上运行代码来检查文件是否存在,并仅返回一小段 HTML,您可以使用
jQuery.load( 将其添加到页面中) )
。Serving a single file from the filesystem is the most simple operation a web server can do. If that is already too much, then all other solutions will be worse. Find out why the server takes so long to serve a simple file and fix that.
Note: I'm assuming that the file is small since you say "test file". If it's a big file, the server will actually send it to the client which will need a lot of resources.
What you can try is to add an ASP page to the web site which runs code on the server that checks whether the file is there and just returns a tiny piece of HTML which you can add to the page with
jQuery.load()
.您应该能够使用 JSONP 和 JQuery.ajax() 进行跨域请求工作。使用
jsonp
和jsonpCallback
属性。或者,您可以使用 JQuery.getJSON()。You should be able to use JSONP and JQuery.ajax() to do cross-domain request work. Play with the
jsonp
andjsonpCallback
attributes. Alternatively, you can use JQuery.getJSON().