0

I am writing a asp.net web api web service. Along with the service I am also writing a tester so the web service can be tested.

the tester allows you to post and receive either JSON or XML. I am processing the response using jquery ajax. I am fine using JSON and the below works fine.

the JSON response

{"ItemLabel":
    {"Requisition":"W44DQ18255TS42 ",
    "Nsn":"5999-01-100-5901",
    "FscNiin":"5999011005901 ",
    "Cage":"1CAY9",
    "PartNumber":"",
    "Nomen":"CONTACT,ELECTRICAL ",
    "Quantity":"1",
    "Ui":"EA",
    "UiDesc":"",
    "PurchOrderNbr":"SPM907-85-5-4444",
    "RlseNbr":"TS0042",
    "Clin":"0042 ",
    "Lot":"",
    "Preservation":"",
    "DatePreserved":"",
    "ShelfType":"",
    "Shelf1":"",
    "Exp1":"",
    "CureDt1":"",
    "CureInsp1":"",
    "Shelf2":"",
    "Exp2":"",
    "CureDt2":"",
    "CureInsp2":"",
    "Serials":"",
    "Serial":null,
    "SerialInters":null,
    "UnitPerInt":"1",
    "TypeLbl":"ITEMx1"
    },
"filePaths":["https://xxxxxxxxxxx.dir.ad.dla.mil/pdf/email_ITEMLBL__W44DQ18255TS42 _682895.pdf"]
}

and I can process the results using jquery ajax as follows.

 success: function (result) {
                            $('#Spinner129').hide();
                            self.jsonResponse129(JSON.stringify(result));
                            self.hasSuccess129(true);
                            self.successMessage129("Success! Please view the response in the JSON Response tab below.");
                            $.each(result.filePaths, function (i, path) {
                                window.open(path, '_blank');                               
                            });
                        },

although I am struggling a bit to do the same thing with the xml response how do I get the values in filepaths?

here is the xml response

<FobOriginWebService129PLabelOutput xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/VSM">
    <ItemLabel>
        <Cage>1CAY9</Cage>
        <Clin>0042  </Clin>
        <CureDt1></CureDt1>
        <CureDt2></CureDt2>
        <CureInsp1></CureInsp1>
        <CureInsp2></CureInsp2>
        <DatePreserved></DatePreserved>
        <Exp1></Exp1>
        <Exp2></Exp2>
        <FscNiin>5999011005901      </FscNiin>
        <Lot></Lot>
        <Nomen>CONTACT,ELECTRICAL </Nomen>
        <Nsn>5999-01-100-5901</Nsn>
        <PartNumber i:nil="true" />
        <Preservation></Preservation>
        <PurchOrderNbr>SPM907-85-5-4444</PurchOrderNbr>
        <Quantity>1</Quantity>
        <Requisition>W44DQ18255TS42 </Requisition>
        <RlseNbr>TS0042</RlseNbr>
        <Serial i:nil="true" />
        <SerialInters i:nil="true" />
        <Serials></Serials>
        <Shelf1></Shelf1>
        <Shelf2></Shelf2>
        <ShelfType>SHL0</ShelfType>
        <TypeLbl>ITEMx1</TypeLbl>
        <Ui>EA</Ui>
        <UiDesc></UiDesc>
        <UnitPerInt>1</UnitPerInt>
    </ItemLabel>
    <filePaths xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <d2p1:string>https://xxxxxxxxxxx.dir.ad.dla.mil/pdf/email_ITEMLBL__W44DQ18255TS42 _405955.pdf</d2p1:string>
       </filePaths>
</FobOriginWebService129PLabelOutput>

not sure how to process it in the jquery ajax success section here was my attempt.

 success: function (result) {
                            $('#Spinner129').hide();
                            self.hasSuccess129(true);                          
                            self.successMessage129("Success! Please view the response in the XML Response tab below.");
                            self.xmlResponse129(JSON.stringify(result));
                           // xmlDoc = $.parseXML(result),
                         //  $xml = $(xmlDoc),
                          // $filePath = $xml.find("filePaths");
                          //  now what?
                        },

2 Answers 2

1
+50

The file path is nested within the children of filePaths node.

<filePaths xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
   <d2p1:string>https://xxxxxxxxxxx.dir.ad.dla.mil/pdf/email_ITEMLBL__W44DQ18255TS42 _405955.pdf</d2p1:string>
</filePaths>

Once you have $filePaths as following:

$filePaths = $xml.find("filePaths");

You can access its children and then get its text:

$filePaths.children().each(function () { 
    //console.log($(this).text()); // print to test
    // https://xxxxxxxxxxx.dir.ad.dla.mil/pdf/email_ITEMLBL__W44DQ18255TS42 _405955.pdf

    // open in new window
    window.open($(this).text(), '_blank');         
});
Sign up to request clarification or add additional context in comments.

Comments

0

Everything looks fine. To get filePath value try this:

$filePath.text();

2 Comments

except their can be multiple. I am trying to replicate $.each(result.filePaths, function (i, path) { window.open(path, '_blank'); });
$filePath.each(function() { console.log($(this).text() });

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.