1

I want to get the <title> of a page using its URL. For example, if the URL is (https://www.google.co.kr/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=ajax%20fail), what I want to get is (ajax fail - google search)

Here is my code and unfortunately it does not work. This code return error code: 0, undefined, no transport.

$(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);
}
pixelistik
  • 7,541
  • 3
  • 32
  • 42
cho
  • 9
  • 3
  • 1
    Possible duplicate of http://stackoverflow.com/questions/7901760/how-can-i-get-the-title-of-a-webpage-given-the-url-an-external-url-using-jquer – Brett Gregson Jan 23 '16 at 09:43

1 Answers1

0

Change, var matches = data.match(/(.*?)</title>/);

to

var matches = html.match(/(.*?)</title>/);

in your request function .

and put,

dataType: "html",

in your ajax request .

AGM Tazim
  • 2,213
  • 3
  • 16
  • 25