English 中文(简体)
跨部门要求
原标题:Cross-Domain Requests with jQuery

For a project I need to get the source code of web page of different other domains. I have tried following code:

$( #container ).load( http://google.com );

$.ajax({
    url:  http://news.bbc.co.uk ,
    type:  GET ,
    success: function(res) {
        var headline = $(res.responseText).find( a.tsh ).text();
        alert(headline);
    }
});

不过,我没有取得任何成果,只是一个空白的警示箱。

最佳回答

否则,所有浏览器都会限制交叉申请,你可以将YQL用作代理。 See a Guide here:

问题回答

由于安全原因,密码能够从其他领域获取内容。 HTTP接入控制,但下线是,如果网站本身没有增加对交叉要求的支持,你会重新改写。

This code is Working Perfectly with the help of JQuery and YQL

$(document).ready(function(){
  var container = $( #target );
  $( .ajaxtrigger ).click(function(){
    doAjax($(this).attr( href ));
    return false;
  });
  function doAjax(url){
    if(url.match( ^http )){
      $.getJSON("http://query.yahooapis.com/v1/public/yql?"+
                "q=select%20*%20from%20html%20where%20url%3D%22"+
                encodeURIComponent("http://www.yahoo.com")+
                "%22&format=xml &callback=?",
        function(data){
          if(data.results[0]){
            var data = filterData(data.results[0]);
            container.html(data);

          } else {
            var errormsg =  <p>Error: could not load the page.</p> ;
            container.html(errormsg);
          }
        }
      );
    } else {
      $( #target ).load(url);
    }
  }
  function filterData(data){
    data = data.replace(/<?/body[^>]*>/g,  );
    data = data.replace(/[
|
]+/g,  );
    data = data.replace(/<--[Ss]*?-->/g,  );
    data = data.replace(/<noscript[^>]*>[Ss]*?</noscript>/g,  );
    data = data.replace(/<script[^>]*>[Ss]*?</script>/g,  );
    data = data.replace(/<script.*/>/,  );
    return data;
  }
});

The solution for your case is JSON with padding or JSONP.

您将需要一个超文本元素,该元素因其弧性特性而具体列出,使JSON返回:

<script type="text/javascript" src="http://differentDomain.com/RetrieveUser?UserId=1234">

你可以在线寻求更深入的解释,但日本人民党肯定是你解决这一问题的办法。

Do the following steps. 1: Add datatype:jsonp to the script. 2: Add a "callback" parameter to the url 3: Create a javascript function with name same as "callback" param value. 4: The output can be received inside javascript function.

www.un.org/Depts/DGACM/index_spanish.htm • 找到另一个解决办法:

function getData(url){
   if(url.match( ^http )){
     $.get(url,
      function(data){
        process(data);
      }//end function(data)
     );//end get
   } 
}

这确实是处理交叉主要要求的一个非常容易的方法。 由于www.imdb.com等一些网站拒绝YQL的请求。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签