javascriptjqueryajaxxmlcxml-commercexml

Remove single quotes from beginning and end of string


I'm trying to remove single quotations from a raw text variable and post via AJAX.

I'm getting this error on return from the API endpoint: Invalid document: Content is not allowed in prolog.

This error is caused when your XML POST data is preceded by a "" or '' usually as is the case with mine. So all you have to do is remove the first and last "" or '' with some simple regex and .trim or .replace.

For whatever reason, it's not removing it for me. I have tried countless regex examples online that supposedly trim the first and last character, only the first and last character if they are "" or '' without any success.

Code:

$('#idealMatBtn').click(function (e) {
    var xmlSTR = '<?xml version="1.0" encoding="UTF-8"?>'
               + '<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.014/cXML.dtd">'
                 + '<cXML timestamp="2015-06-12T08:30:00" xml:lang="en-US">'
                   + '<Header>'
                     + '<From>'
                       + '<Credential domain="NetworkID">'
                         + '<Identity>EU019985</Identity>'
                       + '</Credential>'
                     + '</From>'
                     + '<To>'
                       + '<Credential domain="DUNS">'
                         + '<Identity>Ideal Supply Test</Identity>'
                       + '</Credential>'
                     + '</To>'
                     + '<Sender>'
                       + '<Credential domain="NetworkID">'
                         + '<Identity>Ideal Supply Test</Identity>'
                         + '<SharedSecret>Ideal</SharedSecret>'
                       + '</Credential>'
                       + '<UserAgent>eProcurement-System 1.0</UserAgent>'
                     + '</Sender>'
                   + '</Header>'
                   + '<Request>'
                     + '<PunchOutSetupRequest operation="create">'
                       + '<BuyerCookie>[Unique-Generated-Identifier-from-eProcurement-System]</BuyerCookie>'
                       + '<Extrinsic name="FirstName">John</Extrinsic>'
                       + '<Extrinsic name="LastName">Smith</Extrinsic>'
                       + '<Contact role="endUser">'
                         + '<Name xml:lang="en-US">john</Name>'
                         + '<Email>smith+john@greenwingtechnology.com</Email>'
                       + '</Contact>'
                       + '<BrowserFormPost>'
                         + '<URL>https://test-sys.greenwingtech-system.com/punchout/return</URL>'
                       + '</BrowserFormPost>'
                     + '</PunchOutSetupRequest>'
                   + '</Request>'
                 </cXML>';
    xmlSTR = xmlSTR.toString().replace(/(^"|"$)/g, '');
    $.ajax({
        type : "POST",
        dataType : "xml",
        url : "https://postDataToThisURL.do",
        data : "xmlSTR",
        contentType : "text/xml",
        cache : false,
        processData : false,
        success: function (data) {
            if (data) {
            url = $(data).find("URL").text();
            console.log(data)
            console.log(url)
            window.open(url, "popupWindow", "width=1000,height=600,scrollbars=yes");              
            }
         else {
            // do something
         }
        }
    });
    e.preventDefault();
});

Solution

  • Remove the quotes from this line:

    data : "xmlSTR",
    

    ...to make it:

    data : xmlSTR,
    

    The way you had it, you were setting the data value to the literal string "xmlSTR", i.e., the characters x, m, l, S, T, R. You want to set it to the variable xmlSTR.