简体   繁体   English

在 Jquery/Javascript 中检查 URL

[英]Checking a Url in Jquery/Javascript

All I need is a method that returns true if the Url is responding.我所需要的只是一个在 URL 响应时返回 true 的方法。 Unfortunately, I'm new to jQuery and it's making my attempts at writing that method rather frustrating.不幸的是,我是 jQuery 的新手,它让我编写该方法的尝试相当令人沮丧。

I've seen several examples of jQuery using .ajax, but the code is consistently failing on me.我已经看过几个使用 .ajax 的 jQuery 示例,但是代码一直在我身上失败。 What's wrong?怎么了?

var urlExists = function(url){
    //When I call the function, code is still executing here.
    $.ajax({
        type: 'HEAD',
        url: url,
        success: function() {
            return true;
        },
        error: function() {
            return false;
        }            
    });
    //But not here...
}

That isn't how AJAX works.这不是 AJAX 的工作方式。 AJAX is fundamentally asynchronous (that's actually what the first 'A' stands for), which means rather than you call a function and it returns a value, you call a function and pass in a callback, and that callback will be called with the value. AJAX 本质上是异步的(这实际上是第一个 'A' 所代表的),这意味着您调用一个函数并传入一个回调,而不是调用一个函数并返回一个值,然后将使用该值调用该回调.

(See http:\/\/en.wikipedia.org\/wiki\/Continuation_passing_style<\/a> .) (参见http:\/\/en.wikipedia.org\/wiki\/Continuation_passing_style<\/a> 。)

What do you want to do after you know whether the URL is responding or not?知道 URL 是否响应后,您想做什么? If you intended to use this method like this:如果您打算像这样使用此方法:

//do stuff
var exists = urlExists(url);
//do more stuff based on the boolean value of exists

urlExists()<\/code> can not return because it needs wait for the request. urlExists()<\/code>无法返回,因为它需要等待请求。

Either pass it a callback, or make it synchronous (not recommended, because it locks the browser).要么给它一个回调,要么让它同步(不推荐,因为它会锁定浏览器)。

var urlExists = function(url, callback) {

    if ( ! $.isFunction(callback)) {
       throw Error('Not a valid callback');
    }   

    $.ajax({
        type: 'HEAD',
        url: url,
        success: $.proxy(callback, this, true),
        error: $.proxy(callback, this, false)      
    });

};

If the url is from the same domain as your page you can do it.如果 url 来自与您的页面相同的域,您可以这样做。 But if it is from a different domain, for example google.com, then it will fail due to cross domain security.但是如果它来自不同的域,例如 google.com,那么由于跨域安全性,它将失败。

In general, you should probably run your script in Firefox using the firebug plugin.一般来说,您可能应该使用 firebug 插件在 Firefox 中运行您的脚本。 It will give you the details needed to solve the issue.它将为您提供解决问题所需的详细信息。

The ajax and post methods are asynchronous, so you should handle the result in a callback method. ajax 和 post 方法是异步的,因此您应该在回调方法中处理结果。

AJAX is basically asynchronous, and that's why the behavior you are describing. AJAX 基本上是异步的,这就是您所描述的行为的原因。 I've used the following, which is free of cross origin, to get a simple true\/false indication whether a URL is valid, in a synchronous manner:我使用了以下内容,它是免费的,以同步方式获得一个简单的真\/假指示 URL 是否有效:

function isValidURL(url) {
    var encodedURL = encodeURIComponent(url);
    var isValid = false;

    $.ajax({
      url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + encodedURL + "%22&format=json",
      type: "get",
      async: false,
      dataType: "json",
      success: function(data) {
        isValid = data.query.results != null;
      },
      error: function(){
        isValid = false;
      }
    });

    return isValid;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM