Search code examples
javascriptxmlpostp5.jsstringify

How to post xml data(for eg. a xml file) from the p5.js JavaScript library using the httpPost() function?


I am trying to load in an XML file saved locally on my computer using the loadXML() function in p5.js, and then using the httpPost() function to POST the same to a server which expects an XML file through the following code:

var url = "http://localhost:9000";
var file;
function preload(){
 file = loadXML("filename,xml"); 
 }
function setup(){
 httpPost(url,"xml",file,success,failure);

 }
 function success(){}//some stuff to run when it is successful 
 function failure(){}// when an error is produced.

But I keep getting the following error:

p5.js:59782 Uncaught (in promise) TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at p5.httpDo (p5.js:59782)
    at p5.httpPost (p5.js:59634)
    at setup (sketch.js:20)
    at p5.<anonymous> (p5.js:46215)
    at _runIfPreloadsAreDone (p5.js:46163)
    at p5._decrementPreload (p5.js:46173)
    at p5.js:59339
    at p5.js:59851

error

The xml file:

<?xml version="1.0" encoding="UTF-8"?>
        <ENVELOPE>
            <HEADER>
                <VERSION>1</VERSION>
                <TALLYREQUEST>Export</TALLYREQUEST>
                <TYPE>Data</TYPE>
                <ID>List of Ledgers</ID>
            </HEADER>
        <BODY>
        <DESC>
        <TDL>
        <TDLMESSAGE>
        <REPORT NAME="List of Ledgers" ISMODIFY="No" ISFIXED="No" ISINITIALIZE="No" ISOPTION="No" ISINTERNAL="No">
        <FORMS>List of Ledgers</FORMS>
        </REPORT>
        <FORM NAME="List of Ledgers" ISMODIFY="No" ISFIXED="No" ISINITIALIZE="No" ISOPTION="No" ISINTERNAL="No">
        <TOPPARTS>List of Ledgers</TOPPARTS>
        <XMLTAG>"List of Ledgers"</XMLTAG>
        </FORM>
        <PART NAME="List of Ledgers" ISMODIFY="No" ISFIXED="No" ISINITIALIZE="No" ISOPTION="No" ISINTERNAL="No">
        <TOPLINES>List of Ledgers</TOPLINES>
        <REPEAT>List of Ledgers : Collection of Ledgers</REPEAT>
        <SCROLLED>Vertical</SCROLLED>
        </PART>
        <LINE NAME="List of Ledgers" ISMODIFY="No" ISFIXED="No" ISINITIALIZE="No" ISOPTION="No" ISINTERNAL="No">
        <LEFTFIELDS>List of Ledgers</LEFTFIELDS>
        </LINE>
        <FIELD NAME="List of Ledgers" ISMODIFY="No" ISFIXED="No" ISINITIALIZE="No" ISOPTION="No" ISINTERNAL="No">
        <SET>$Name</SET>
        <XMLTAG>"NAME"</XMLTAG>
        </FIELD>
        <COLLECTION NAME="Collection of Ledgers" ISMODIFY="No" ISFIXED="No" ISINITIALIZE="No" ISOPTION="No" ISINTERNAL="No">
        <TYPE>Ledger</TYPE>
        </COLLECTION>
        </TDLMESSAGE>
        </TDL>

        </DESC>
        </BODY>
        </ENVELOPE>

Edit 1: I tried using the loadStrings() function which worked perfectly fine with the httpPost method using the following code -

list = loadStrings("ListLedgers.xml");    
function setup(){  
list = list.join('');  
httpPost(url,"xml",list,yay,oops);  
}  

Solution

  • If I were you I would try to get something simpler working. Can you get this working with a more basic piece of XML? Can you hard-code the XML and see if that works?

    If you google your error "Converting circular structure to JSON at JSON.stringify" you'll get a ton of results. Basically you can't convert an object to JSON if it contains circular references.

    Then looking at the P5.js reference, you'll notice that the p5.XML type contains a circular reference with its getParent() function.

    This is the cause of your error: the httpPost() function is trying to convert your XML into JSON, which it can't do because it contains a circular reference.

    To fix your problem, you have a few options:

    • Track down why the httpPost() function is converting XML to JSON. This smells like a bug, so I'd do some more debugging to figure out what's going on.
    • You could convert your XML into a string. The getContent() function might help here.
    • You could load your XML as text or as bytes instead of loading it as XML.

    The above are just guesses because I can't actually run your code, but hopefully this gets you unblocked. Good luck.