tags:

views:

111

answers:

2

I am using jquery ajax $.get which calls a php script on my server and retrieves content from another site (domain). It return's the full web page ok. I then want to extract html data from the 'center' tag using the .find() method, but I am having some issues.

$("#btnGetData").click(function(){
  $.get("pgiproxy.php",{ data: $("#formdata").serialize() },
    function(result) {

      alert($(result).find('center').html()); 

      val = $(result).find('center').html();
      alert(val);

      $('#BFX').html(result);
      alert( $('#BFX').find('center').html() );  

    });
});

The first 2 methods both return 'null', but when I insert the ('result') html into the #BFX div it displays correctly on my monitor, the final alert works correctly, it find()s the 'center' tag and displays the html data in the alert.

Can anybody see why I get 'null' returned (as in the first 2 alerts) when trying to 'find()' the 'center' tag from the returned ajax data 'result'.

Any help is appreciated

Nic.

A: 

The contents of result here are going to depend on the MIME type of the response, as mentioned here. If result is just text, the $() function is going to treat it as a selector.

Syntactic
hi Syntactic, this is the code used in my php script which return the result for jquery ajax call:function getPage($pageURL) { // Set your return content type header('Content-type: text/html; charset=utf-8'); // Get that website's content $handle = fopen($pageURL, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 4096); echo $buffer; } fclose($handle); }}I assume the result is just text??? I am happy to put the result into a hidden div then extract the data from the 'center' tag, but it would be nice to do it directly.
niczoom
+1  A: 

you should try this:

$("#btnGetData").click(function(){
  $.get("pgiproxy.php",{ data: $("#formdata").serialize() },
    function(result) {
      var temp = $('<div/>').html(result);

      alert(temp.find('center').html()); 

      val = temp.find('center').html();
      alert(val);

      $('#BFX').html(result);
      alert( $('#BFX').find('center').html() );  

    });
});
vsync
hi vsnyc, yes this method worked. But why do I need to put the data into the 'temp' variable first before I 'find('center').html()'. Why cant I just do a 1 step method and extract 'center' directly from 'result'??? Nic.
niczoom
Because jQuery needs to traverse a DOM object or jQuery object, not a JSON object, or whatever the response it..which I take it, is not DOM one. this is the approach when doing these things, to convert the response to an element we can work on.
vsync
ok thanks vsync. The response comes from a PHP script which uses 'fopen and fgets' to grab the contents of a web page, which is returned as just 'text' I assume?.Is $('<div/>') in "" var temp = $('<div/>').html(result); "" just used as a placeholder to convert the 'result' to a usable jQuery object?
niczoom
yes, it's just a placeholder, it's not an element which is really in the DOM.
vsync