简体   繁体   English

从字符串的开头和结尾删除单引号

[英]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. 我正在尝试从原始文本变量中删除单引号,然后通过AJAX发布。

I'm getting this error on return from the API endpoint: Invalid document: Content is not allowed in prolog . 我从API端点返回时遇到此错误: 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. 当您的XML POST数据前面带有""''通常是我的情况),就会导致此错误。 So all you have to do is remove the first and last "" or '' with some simple regex and .trim or .replace . 因此,您要做的就是使用一些简单的正则表达式和.trim.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();
});

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" , ie, the characters x, m, l, S, T, R. You want to set it to the variable xmlSTR . 拥有的方式是,将data值设置为文字字符串"xmlSTR" ,即字符x,m,l,S,T,R。您要将其设置为变量 xmlSTR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM