Jquery returns null - why?

I am using jquery ajax $ .get which calls a php script on my server and fetches content from another site (domain). It returns the full webpage ok. Then I want to extract html data from the "center" tag using the .find () method, but I am having some problems.

$("#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 two methods return 'null', but when I insert ('result') html in the #BFX div, it displays correctly on my monitor, the final alert works correctly, it finds () s 'center' and displays the html data in the post.

Can anyone understand why I am returning "null" (as in the first two warnings) when trying to "find ()" the "center" tag from the "ajax" return result.

Any help is appreciated

Nic.

+2


a source to share


2 answers


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

    });
});

      

+2


a source


The content result

here will depend on the MIME response type as mentioned here . If result

- only text, the function $()

will treat it as a selector.



0


a source







All Articles