jQuery / js可以帮助我确定URL是否存在 [英] Can jQuery/js help me determine if an URL exists

查看:64
本文介绍了jQuery / js可以帮助我确定URL是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄明白,jQuery / Ajax可以帮助我确定给定的URL是否在线。

I can't figure out wheater or not, jQuery/Ajax can help me determine if a given URL is online.

我真正想要的是HTTP的http连接的HTTP状态代码,但可能要求太多了吗?

What I really is looking for is the HTTP status code of a http connnection to the URL, but it may be too much to ask?

网址是用户输入,因此我不可能修改我连接的服务器上的环境。

The urls are user input, and thus it is not possible for me to modify the environment on the server I am connecting to.

每个帮助都非常受欢迎。

Every help is more than welcome.

推荐答案

这在浏览器中是不可能的,但是如果你要向你的网络服务器发出ajax请求,你可以要求它进行验证。

This is not possible inside of the browser but if you were to make an ajax request to your webserver, you could ask it do do the verification.

客户代码

$.ajax({
    url: "urltest.py",
    cache: false, 
    dataType: "text"
    success: function(data){
        if(data === "SUCCESS"){
        //do things
        } else {
        //else do other things
        }
    }
});

服务器代码

def call_server(self, url):
    headers = {'Content-Type': 'application-json'
              , 'Accept' : 'application-json'
              , 'Accept-Encoding' : 'gzip, deflate'
              }
    r = requests.get(url)
    if r.status_code == 200:
        print "SUCCESS"
    else:
        print "No soup for you"

尚未经过测试或其他任何事情,但你明白了。

Hasn't been tested or anything, but you get the idea.

这篇关于jQuery / js可以帮助我确定URL是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆