0

I'm currently just trying to test and find out how to pull information from cnn.com and get the some of the titles of the articles with the class name, "cd__headline-text." However, when I use the $.ajax function to get the titles of the articles on cnn with that class name I get an error that says response.getElementsByClassName is not a function.

Below is the code that prompts this error to happen:

$(document).ready(function(){
    $("button").click(function(){
    console.log("hi test");
    $.ajax({
        url: "https://www.cnn.com",
        cache: false,
        success: function(response){
            filter = response.getElementsByClassName("cd__headline-text");
            console.log(filter);
        }
    });

    });
});

My current console.log(response); output is in this link:

https://pastebin.com/SsBSPdBL

3
  • console.log(response); will be your friend Commented Nov 10, 2017 at 5:52
  • You need to use DOMParser Commented Nov 10, 2017 at 6:00
  • @ravisachaniya I tried using DOMParser, but it did not work. Instead in the console it showed me this [prevObject: r.fn.init(100)] Commented Nov 10, 2017 at 6:30

2 Answers 2

0

However, when I use the $.ajax function to get the titles of the articles on cnn with that class name I get an error that says response.getElementsByClassName is not a function.

Because response is not a Node or DOM element.

You need to parse the XML and find the element by attribute

xmlDoc = $.parseXML( response ),
$xml = $( xmlDoc ),

and now get the required value from it

$title = $xml.find( "[class='cd__headline-text']" );

or

$title = $xml.find( ".cd__headline-text" );

Or if the value is already an HTML, then simply

 $( response ).find( ".cd__headline-text" )
Sign up to request clarification or add additional context in comments.

8 Comments

The XML did not work, and neither did the HTML value. Could you try doing this with a website, and perhaps demo it and then get back to me if you find out how to do this.
can you put the response value in your question?
After trying the HTML answer, I get this: [prevObject: r.fn.init(100)]
Response that you are getting needs to be examined. Add the same in your question, preferably create a snippet of the same using <>.
@RajatKhare Links can be broken, create a snippet here itself. Also, I can't access pastebin.
|
0

Try this, or check URL

 $(document).ready(function(){
    $("button").click(function(){
    console.log("hi test");
    $.ajax({
       url: "https://www.cnn.com",
       cache: false,
       success: function(response){
           $(response).find('.cd__headline-text').each(function(){
              console.log($(this).html());
           });
      }
    });
    });
});

Hope this will help you.

5 Comments

Hi, I tried your suggestion, but nothing gets printed in the console. Do you know why?
No 'Access-Control-Allow-Origin' header is present on the requested resource. I am facing an error on the console while trying your content. check in my fiddle
Now check I tried my URL, and it's working perfectly. I have given 'Access-Control-Allow-Origin' permission in my link.
so how do I get the titles from cnn.com then?
check this once- URL

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.