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.

3
  • I'm not used to jquery, but result.find('center').html() should not work? Commented May 12, 2010 at 13:33
  • did you dump the contents of 'result' ? you may try to set the datatype after your callback to 'html' $.get('foo', {}, function(){}, 'html'); Commented May 12, 2010 at 13:37
  • Hi jAndy, I set the datatype to 'html', still the same result using -- alert($(result).find('center').html()); --, returns null. Commented May 12, 2010 at 14:07

2 Answers 2

2

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() );  

    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
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?
yes, it's just a placeholder, it's not an element which is really in the DOM.
0

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.