0

Ok, I am officially stumped. I have been trying to find why my calls for specific items in a PubMed xml data file are not working... I can execute this one with my current coding:

$test = (string)$id_json->PubmedArticle->MedlineCitation->PMID;

but if I try to get a variable that is in a deeper array, it does not return a value. I have even tested with console.log(data) and I get my PMID returning but not my other, deeper values in the XML file. For example;

$test = (string)$id_json->PubmedArticle->MedlineCitation->Article->Journal->ISSN;

returns nothing for data in console.log(data)

Here is my function in wordpress:

function get_abstract(){
$id = $_POST['abstractid'];

$pubmed_api_call = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&rettype=abstract&id='.$id;

$id_wpget = wp_remote_get($pubmed_api_call, array('timeout' => 20));
if( is_wp_error( $id_wpget ) ) {
    echo "Error Contacting PubMed, please refresh page and try again";
 die();
}
$id_xml = wp_remote_retrieve_body($id_wpget);
$id_json = simplexml_load_string($id_xml);
$test = (string)$id_json->PubmedArticle->MedlineCitation->Article->Journal->ISSN;
if($test === ""){
echo "NOTHING";
die();
}

echo $test;
die();
}

and here is my javascript AJAX call:

jQuery(document).ready(function() {

  jQuery('.reference_header').click(function(e) {
    jQuery(this).find("i").toggleClass("arrow-down arrow-up");
    jQuery(this).nextUntil('.reference_header').slideToggle('fast');
    var abstractid = jQuery(this).data("id");
    e.preventDefault();
        jQuery.ajax({
            url: get_abstract.ajaxurl,
            type: 'POST',
            dataType: 'json',
            data: {
        abstractid: jQuery(this).data("id"),
        action: 'get_abstract'
        },
            success : function(data){
                    jQuery('.'+abstractid).html("TESTING: "+data);
                    console.log(data);
                }

        });
  });

});

I cannot find out why it doesnt work... any help is greatly appreciated.

2
  • You may want to convert the XML string to an array and parse: [link]stackoverflow.com/questions/6578832/… Commented Oct 30, 2018 at 14:16
  • Just to clarify: initially you didn't get your 'deeper' request's data, because that was a mixed object (object + array(s), and not only object? That's why the serialized string object was unable to retrieve that without encoding to json? (if you still remember this :) Cheers Commented Mar 28, 2021 at 20:08

1 Answer 1

1

So I figured out the solution to the issue... you need to pass the string text as a json object to AJAX for it to read properly...

working code:

PHP:

echo json_encode(array("result" => "$test"));
die();

AJAX:

        success : function(data){
                jQuery('.'+abstractid).html("TESTING: "+data.result);
                console.log(data.result);
            }
Sign up to request clarification or add additional context in comments.

Comments

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.