使用jQuery检查另一个域上的URL是否为404? [英] Use jQuery to check if a URL on another domain is 404 or not?

查看:173
本文介绍了使用jQuery检查另一个域上的URL是否为404?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用jQuery的客户端上,我想知道是否可以仅检查链接URL是否有效(即不返回404).该链接指向另一个域,因此,如果我只使用$ .get(),那么我将遇到权限问题.我记得读过一些有关使用JSONP请求的内容,但我不记得了.

On the client side using jQuery, I want to know if I can just check if a link URL is valid (i.e. doesn't return a 404). This link points to another domain, so if I just use $.get() then I end up with a permission issue. I remember reading something about using a JSONP request, but I don't remember.

推荐答案

我发现了一个似乎可行的解决方案(使用YQL):

I found a solution that seems to work (using YQL):

$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
            "q=select%20*%20from%20html%20where%20url%3D%22"+
            encodeURIComponent(url)+
            "%22&format=xml'&callback=?",
    function(data){
      if(data.results[0]){
        // do whatever
      } 
    }
  );

假设您要检查的URL在变量"url"中.

Assumes the URL you want to check is in the variable 'url'.

这篇关于使用jQuery检查另一个域上的URL是否为404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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