jQuery AJAX (google PageRank)
I need a little help..
iv'e been developing a Jqery plug-in to get the page ranks of urls on a website using XHR,
The problem is when requesting the rank from google servers the page is returned no content, but if i use an inspector and get the url that was requests and go to it via my browser the pageranks are shown. so it must be something with headers but its just got me puzzled.
Heres some source code but i have removed several aspects that are not needed to review.
pagerank.plugin.js
(
$.fn.PageRank = function(callback)
{
var _library = new Object();
//Creat the system library
_library.parseUrl = function(a)
{
var b = {};
var a = a || '';
/*
* parse the url to extract its parts
*/
if (a = a.match(/((s?ftp|https?):\/\/){1}([^\/:]+)?(:([0-9]+))?([^\?#]+)?(\?([^#]+))?(#(.+))?/)) {
b.scheme = a[2] ? a[2] : "http";
b.host = a[3] ? a[3] : null;
b.port = a[5] ? a[5] : null;
b.path = a[6] ? a[6] : null;
b.args = a[8] ? a[8] : null;
b.anchor = a[10] ? a[10] : null
}
return b
}
_library.ValidUrl = function(url)
{
var b = true;
return b = url.host === undefined ? false : url.scheme != "http" && url.scheme != "https" ? false : url.host == "localhost" ? false : true
}
_library.toHex = function(a){
return (a < 16 ? "0" : "") + a.toString(16)
}
_library.hexEncodeU32 = function(a) {
}
_library.generateHash = function(a)
{
for (var b = 16909125, c = 0; c < a.length; c++)
{
}
return _library.hexEncodeU32(b)
}
var CheckPageRank = function(domain,_call)
{
var hash = _library.generateHash(domain);
$.ajax(
{
url: 'http://www.google.com/search?client=navclient-auto&ch=8'+hash+'&features=Rank&q=info:' + escape(domain),
async: true,
dataType: 'html',
ifModified:true,
contentType:'',
type:'GET',
beforeSend:function(xhr)
{
xhr.setRequestHeader('Referer','http://google.com/'); //Set Referer
},
success: function(content,textS,xhr){
var d = xhr.responseText.substr(9, 2).replace(/\s$/, "");
if (d == "" || isNaN(d * 1)) d = "0";
_call(d);
}
});
}
//Return the callback
$(this).each(function(){
urlsegments = _library.parseUrl($(this).attr('href'))
if(_library.ValidUrl(urlsegments))
{
CheckPageRank(urlsegments.host,function(rank){
alert(rank)
callback(rank);
});
}
});
return this; //Dont break any chain.
}
)(jQuery);
Index.html (example)
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="pagerank.plugin.js"></script>
&l开发者_StackOverflowt;script type="text/javascript">
$(document).ready(function() {
$('a').PageRank(function(pr){
alert(pr);
})
});
</script>
</head>
<body>
<a href="http://facebook.com">a</a>
<a href="http://twitter.com">a</a>
<div></div>
</body>
</html>
i just cant understand why its doing this.
--
Notes:
using and XHR Outside of jquery works just fine!
function getPageRank(a, b) {
a = "http://www.google.com/search?client=navclient-auto&ch=8" + awesomeHash(a) + "&features=Rank&q=info:" + a;
var c = new XMLHttpRequest;
c.open("GET", a, true);
c.onreadystatechange = function () {
if (c.readyState == 4) {
console.log("reponse text is " + c.responseText);
var d = c.responseText.substr(9, 2).replace(/\s$/, "");
if (d == "" || isNaN(d * 1)) d = "0";
b(d)
}
};
c.send()
}
You cannot fetch any content from a different server (than the one where the page is hosted) using AJAX. Browsers prohibit this explicitly as a security measure.
The best you can do is make an AJAX request to a server-side script hosted on your own server and let that script communicate with Google.
for safety,no browser allow ajax conneting annother domain, otherwise, Lot of google will appear,weber can host a "html"page to service ?
but json can do,it runtimely insert a script on a page, and the script src property is set.
google page rank api only return a text like "1:1:5" ,which can not be explained by scripter,thus all gone!
So,ajax can`t do it.
精彩评论