Brian
Brian

Reputation: 4408

How do I parse a remote website using ajax

I'm trying to parse through the DOM of a remote website using JSONP I think the problem is that when I get the response its not JSON so the deferred object fails, but then can't I just take the raw data there and traverse the dom nodes somehow? $(document).ready(function() {

                    $('#submitUrl').on('click', function() {

                          var words = {};

                          var url = $('#url').val();


                          //send a JSONP request since its the only cross domain method available
                          var options = {
                                dataType: "JSONP",
                                type: 'GET',
                                url: url,
                          };
                          var arr = new Array();
                          var countArr = new Array();

                          var jqxhr = $.ajax(options)
                                  .done(function(data) {
                                //this will always fail since jquery expects valid json response, try parsing in always pipe

                          })
                                  .fail(function() {

                          })
                                  .always(function(data) {


                                var fileDom = $(data);
                                //grab each element under the body dom element
                                //probably a filter method would work faster
                                fileDom.find('body *').each(function(index, value) {
                                      if ($(this).text())
                                      {
                                            var txt = $(this).text();
                                            //is this in the array already
                                            if ($.inArray(txt, arr)) {
                                                  //get current ocunt
                                                  var c = parseInt(countArr[txt]);
                                                  c++;
                                                  countArr[txt] = c;
                                            }
                                            else {
                                                  countArr[txt] = 1;

                                            }



                                      }


                                });
                          });

                    });

              });

html

    <label>Input URL</label>
                                <input type="text" name="url" id="url" value=""/>
                                <input type="submit" id="submitUrl">

Upvotes: 0

Views: 196

Answers (1)

zerkms
zerkms

Reputation: 254916

You cannot do that.

For being able to use jsonp - the remote side should give the response in a special format (wrapped in a callback function).

So it's impossible by definition to use jsonp with an arbitrary web page.

Upvotes: 1

Related Questions