[英]How to use ajax to get page title for a URL
我想使用其URL獲取頁面的<title>
。 例如,如果URL是( https://www.google.co.kr/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=ajax%20fail ),那么我想要得到的是( Ajax失敗-Google搜索)
這是我的代碼,不幸的是它不起作用。 此代碼返回錯誤代碼:0,未定義,無傳輸。
$(document).ready(function () {
titleInfo = {
url: "http://google.co.kr",
title: "abcd"
}
$('#target').text("SS");
requestAjax('http://gorapaduk.dothome.co.kr', titleInfo);
});
var requestAjax = function (url, data) {
$.ajax({
url: url,
cache: false,
success: function (html) {
alert(html);
request(html, data);
},
error:function(request,status,error){
alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
}
});
}
var request = function (html, data) {
var matches = data.match(/<title>(.*?)<\/title>/);
var spUrlTitle = matches[1];
data.title = spUrlTitle;
$('#target').text("spUrlTitle");
console.log(data.title);
}
更改,var matchs = data.match(/(.*?)</ title> /);
至
var matchs = html.match(/(.*?)</ title> /);
在您的請求功能中。
然后放
dataType:“ html”,
在您的ajax請求中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.