开发者

Check server response in javascript

开发者 https://www.devze.com 2022-12-31 15:47 出处:网络
I want to check server response in javascript. For example I have a server A which will host the script. On excuting the script it will check if the server B is responding or not. If yest continue oth

I want to check server response in javascript. For example I have a server A which will host the script. On excuting the script it will check if the server B is responding or not. If yest continue other wise redirect to server C. Is this possible with Javascript/Jquery? If not what could be a possible开发者_JAVA技巧 solution in PHP?


You could make an ajax request to a known interface in each server. If the success callback is called, then the server is up and running. Otherwise (error, timeout) there is a problem.

Server development may be needed in order to make sure that there is a url you can safely call. Be warned that it could take some time for your script to report the error.


Did You know AJAX doesn't work cross domain? It doesn't. Safety reasons.

So first thing is You have to use jQuery's getJSON or getScript

create an array of servers somewhere and loop through them saving current one somewhere and doing a query. if it responds - continue using saved one. if not - continue searching.

remember that Your calls are asynchronous and You have to make it work with callbacks and query error functions.


for php you can use fsockopen function

0

精彩评论

暂无评论...
验证码 换一张
取 消