'&'的意义JavaScript中的符号

时间:2013-12-04 03:17:57

标签: javascript html sharepoint wsdl

我目前正在处理一个将数据发布到SAP的表单。我使用JavaScript来调用Web服务来执行此操作。我遇到了一个问题,其中表单中的一个字段的值为'GT& N'而我只在'&'时出错符号存在。我已经看过谷歌,但没有找到关于&符号和JavaScript。

我从网页收到错误消息:

意外的文件结束。以下元素未关闭:Characteristics_Value,RunParameter,Run,Body,Envelope。第1行,第2520位

**'characteristic_Value'是表单中的字段。

我在调试时看到错误:

See Screen Shot

每当我不使用'&'时符号,一切都很好。我也试过使用其他特殊字符,它发布得很好。

这里有相当多的代码,所以我不会发布所有内容,除非请求,因为这只是关于'&'的一般性问题。符号和JavaScript。

JavaScript代码

//Create
var WebServer = "http://webserver";
var WebServiceName = "CreateIngredient";
var ScriptType = "Transaction";
var RepeatingTableXMLPath = "/my:myFields/my:CreateIngredient/my:CreateIngredient_Repeat";

// To Call Web service from infopath
function CreateIngredient(theXmlNode) {

//Add parameter into the request
var addBatchRequest = new SOAPClientParameters();
var Addresses = new Array();
var AddressXmlNodes = theXmlNode.selectNodes(RepeatingTableXMLPath);
// Loop for Input Field from Repeating Table
for (var i = 0; i < AddressXmlNodes.length; i++) {
    var xPath = AddressXmlNodes[i].getXPath();
    // Input field, It can be n number of fields
    addBatchRequest.add("Material_description", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Material_description").getValue());
    addBatchRequest.add("Base_Unit_of_Measure", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Base_Unit_of_Measure").getValue());
    addBatchRequest.add("Material_group", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Material_group").getValue());
    addBatchRequest.add("External_Material_Group", AddressXmlNodes[i].selectSingleNode(xPath + "/my:External_Material_Group").getValue());
    addBatchRequest.add("Division", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Division").getValue());
    addBatchRequest.add("Authorization_Group", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Authorization_Group").getValue());
    addBatchRequest.add("Gross_weight", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Gross_weight").getValue());
    addBatchRequest.add("Weight_Unit", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Weight_Unit").getValue());
    addBatchRequest.add("Net_weight", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Net_weight").getValue());
    addBatchRequest.add("Dangerous_Goods_Indicator_Profile", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Dangerous_Goods_Indicator_Profile").getValue());
    addBatchRequest.add("Class_number", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Class_number").getValue());
    addBatchRequest.add("Characteristic_Value", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Characteristic_Value").getValue().toString());
    addBatchRequest.add("Class_number1", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Class_number1").getValue());
    addBatchRequest.add("Plant", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Plant").getValue());
    addBatchRequest.add("Checking_Group_for_Availability_Check", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Checking_Group_for_Availability_Check").getValue());
    addBatchRequest.add("Batch_management_requirement_indicator", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Batch_management_requirement_indicator").getValue());
    addBatchRequest.add("Transportation_group", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Transportation_group").getValue());
    addBatchRequest.add("Loading_group", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Loading_group").getValue());
    addBatchRequest.add("Profit_Center", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Profit_Center").getValue());
    addBatchRequest.add("Purchasing_group", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Purchasing_group").getValue());
    addBatchRequest.add("Plant-Specific_Material_Status", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Plant-Specific_Material_Status").getValue());
    addBatchRequest.add("Tax_indicator_for_material__Purchasing_", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Tax_indicator_for_material__Purchasing_").getValue());
    addBatchRequest.add("Indicator___automatic_purchase_order_allowed_", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Indicator___automatic_purchase_order_allowed_").getValue());
    addBatchRequest.add("Purchasing_Value_Key", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Purchasing_Value_Key").getValue());
    addBatchRequest.add("Storage_location", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Storage_location").getValue());
    addBatchRequest.add("Temperature_conditions_indicator", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Temperature_conditions_indicator").getValue());
    addBatchRequest.add("Label_type", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Label_type").getValue());
    addBatchRequest.add("Label_form", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Label_form").getValue());
    addBatchRequest.add("Minimum_remaining_shelf_life", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Minimum_remaining_shelf_life").getValue());
    addBatchRequest.add("Total_shelf_life", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Total_shelf_life").getValue());
    addBatchRequest.add("Storage_percentage", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Storage_percentage").getValue());
    addBatchRequest.add("Documentation_required_indicator", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Documentation_required_indicator").getValue());
    addBatchRequest.add("QM_in_Procurement_is_Active", AddressXmlNodes[i].selectSingleNode(xPath + "/my:QM_in_Procurement_is_Active").getValue());
    addBatchRequest.add("Control_Key_for_Quality_Management_in_Procurement", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Control_Key_for_Quality_Management_in_Procurement").getValue());
    addBatchRequest.add("Valuation_Class", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Valuation_Class").getValue());
    addBatchRequest.add("Price_control_indicator", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Price_control_indicator").getValue());
    addBatchRequest.add("Price_unit", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Price_unit").getValue());
    addBatchRequest.add("Standard_price", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Standard_price").getValue());
    addBatchRequest.add("Price_unit_for_valuation_prices_based_on_tax_commercial_law", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Price_unit_for_valuation_prices_based_on_tax_commercial_law").getValue());
    addBatchRequest.add("Material-related_origin", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Material-related_origin").getValue());
    addBatchRequest.add("Variance_Key", AddressXmlNodes[i].selectSingleNode(xPath + "/my:Variance_Key").getValue());
    addBatchRequest.add("Lot_Size_for_Product_Costing",     AddressXmlNodes[i].selectSingleNode(xPath + "/my:Lot_Size_for_Product_Costing").getValue());




    // Output field has been field with text Retrieving... in Gray color for presentation purpose only
    var LogFieldNode = AddressXmlNodes[i].selectSingleNode(xPath + "/my:LogField");
    var txtLog = document.getElementById("SV_" + LogFieldNode.getAttribute("SVFormElementId"));
    txtLog.value = "Retrieving...";
    txtLog.style.color = 'Gray';


    // Final web service call parameter
    var pl = new SOAPClientParameters();
    pl.add("RunParameter", addBatchRequest.toXml());
    pl.toXml();
    SOAPClient.invoke(WebServer + "/formsservice.svc/basic", "Run", pl, true,    CreateIngredient_callBack, i);
}
}

// To display the retrieved result in the proper output fields
function CreateIngredient_callBack(u, xmlresponse, RowNumber) {
var theXmlNode1 = SVFormInternalGetProperXmlNode(SVForms[0]);

var AddressXmlNodes1 = theXmlNode1.selectNodes(RepeatingTableXMLPath);
var xPath = AddressXmlNodes1[RowNumber].getXPath();

var LogFieldNode = AddressXmlNodes1[RowNumber].selectSingleNode(xPath + "/my:LogField");
var txtLog = document.getElementById("SV_" + LogFieldNode.getAttribute("SVFormElementId"));
txtLog.style.color = "";
txtLog.value = "";

if (u == null)
    alert("No data retrieved for Row Number " + (RowNumber + 1) + ".");
else {
    //Display result in repeating table

    if (u.LogField) {
        LogFieldNode.setValue(u.LogField);
        document.getElementById("SV_" +  LogFieldNode.getAttribute("SVFormElementId")).value = u.LogField;
    }

}
}


///////////////////// Do not modify below this line ///////////////////////////////

var WsdlResult = null;
//Helping method: To build xml
 function SOAPClientParameters() {
var _pl = new Array();
this.add = function (name, value) {
    _pl[name] = value;
    return _pl;
}

this.toXml = function () {
    var xml = "";
    for (var p in _pl) {
        if (typeof (_pl[p]) != "function")
            xml += "<" + p + ">" + _pl[p].toString() + "</" + p + ">";
    }
    return xml;
}
}

function SOAPClientRepeatingParametersXml(name, values) {
var xml = "";
for (var i = 0; i < values.length; i++) {

    xml += "<" + name + ">" + values[i].toString() + "</" + name + ">";

}
return xml;
}

function SOAPClient() { }

SOAPClient.invoke = function (url, method, parameters, async, callback, RowNumber) {
if (async)
    SOAPClient._loadWsdl(url, method, parameters, async, callback, RowNumber);
else
    return SOAPClient._loadWsdl(url, method, parameters, async, callback, RowNumber);
}

// private: wsdl cache
SOAPClient_cacheWsdl = new Array();

// private: invoke async
SOAPClient._loadWsdl = function (url, method, parameters, async, callback, RowNumber) {
// load from cache?
var wsdl = SOAPClient_cacheWsdl[url];

if (wsdl + "" != "" && wsdl + "" != "undefined")
    return SOAPClient._sendSoapRequest(url, method, parameters, async, callback, wsdl, RowNumber);
// get wsdl
var xmlHttp = SOAPClient._getXmlHttp();
xmlHttp.open("GET", url + "?wsdl", async);
if (async) {
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4)
            SOAPClient._onLoadWsdl(url, method, parameters, async, callback, xmlHttp, RowNumber);
    }
}
xmlHttp.send(null);
if (!async)
    return SOAPClient._onLoadWsdl(url, method, parameters, async, callback, xmlHttp, RowNumber);
}

SOAPClient._onLoadWsdl = function (url, method, parameters, async, callback, req, RowNumber) {
var wsdl = req.responseXML;
SOAPClient_cacheWsdl[url] = wsdl; // save a copy in cache
return SOAPClient._sendSoapRequest(url, method, parameters, async, callback, wsdl, RowNumber);
}

SOAPClient._sendSoapRequest = function (url, method, parameters, async, callback, wsdl, RowNumber) {
// get namespace
try {
    var ns = "http://servername.com/server/";
    // build SOAP request
    var sr =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<soap:Envelope " +
            "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
            'xmlns:api="http://IPAddress/Integrics/Enswitch/API" ' +
            "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
            "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
            "<soap:Body>" +
            "<" + method + " xmlns=\"" + ns + "\">" +
            parameters.toXml() +
            "</" + method + "></soap:Body></soap:Envelope>";
    // send request
    var xmlHttp = SOAPClient._getXmlHttp();
    xmlHttp.open("POST", url, async);
    var soapaction = ((ns.lastIndexOf("/") != ns.length - 1) ? ns + "/" : ns) + method;
    xmlHttp.setRequestHeader("SOAPAction", "http://servername.com/server/I" + ScriptType + "Service/Process(" + WebServiceName + ")");
    xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    if (async) {
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4)
                SOAPClient._onSendSoapRequest(method, async, callback, wsdl, xmlHttp, RowNumber);
        }
    }
    xmlHttp.send(sr);
}
catch (ex) { }
if (!async)
    return SOAPClient._onSendSoapRequest(method, async, callback, wsdl, xmlHttp, RowNumber);
}

SOAPClient._onSendSoapRequest = function (method, async, callback, wsdl, req, RowNumber) {
var o = null;
var nd = SOAPClient._getElementsByTagName(req.responseXML, method + "Result");
if (nd.length == 0) {
    if (req.responseXML.getElementsByTagName("faultcode").length > 0);
    alert(req.responseXML.getElementsByTagName("faultstring")  [0].childNodes[0].nodeValue);
}
else
    o = SOAPClient._soapresult2object(nd[0], wsdl);
if (callback)
    callback(o, req.responseXML, RowNumber);
if (!async)
    return o;
}

// private: utils
SOAPClient._getElementsByTagName = function (document, tagName) {
try {
    // trying to get node omitting any namespaces (latest versions of  MSXML.XMLDocument)
    return document.selectNodes(".//*[local-name()=\"" + tagName + "\"]");
}
catch (ex) { }
// old XML parser support
return document.getElementsByTagName(tagName);
}

SOAPClient._soapresult2object = function (node, wsdl) {
return SOAPClient._node2object(node, wsdl);
}
SOAPClient._node2object = function (node, wsdl) {
// null node
if (node == null)
    return null;
// text node
if (node.nodeType == 3 || node.nodeType == 4)
    return SOAPClient._extractValue(node, wsdl);
// leaf node
if (node.childNodes.length == 1 && (node.childNodes[0].nodeType == 3 ||  node.childNodes[0].nodeType == 4))
    return SOAPClient._node2object(node.childNodes[0], wsdl);
var isarray = SOAPClient._getTypeFromWsdl(node.nodeName,  wsdl).toLowerCase().indexOf("arrayof") != -1;
// object node
if (!isarray) {
    var obj = null;
    if (node.hasChildNodes())
        obj = new Object();
    for (var i = 0; i < node.childNodes.length; i++) {
        var p = SOAPClient._node2object(node.childNodes[i], wsdl);
        obj[node.childNodes[i].nodeName] = p;
    }
    return obj;
}
// list node
else {
    // create node ref
    var l = new Array();
    for (var i = 0; i < node.childNodes.length; i++)
        l[l.length] = SOAPClient._node2object(node.childNodes[i], wsdl);
    return l;
}
return null;
}
SOAPClient._extractValue = function (node, wsdl) {
var value = node.nodeValue;
switch (SOAPClient._getTypeFromWsdl(node.parentNode.nodeName, wsdl).toLowerCase()) {
    default:
    case "s:string":
        return (value != null) ? value + "" : "";
    case "s:boolean":
        return value + "" == "true";
    case "s:int":
    case "s:long":
        return (value != null) ? parseInt(value + "", 10) : 0;
    case "s:double":
        return (value != null) ? parseFloat(value + "") : 0;
    case "s:datetime":
        if (value == null)
            return null;
        else {
            value = value + "";
            value = value.substring(0, value.lastIndexOf("."));
            value = value.replace(/T/gi, " ");
            value = value.replace(/-/gi, "/");
            var d = new Date();
            d.setTime(Date.parse(value));
            return d;
        }
}
}
SOAPClient._getTypeFromWsdl = function (elementname, wsdl) {
var ell = wsdl.getElementsByTagName("s:element"); // IE
if (ell.length == 0)
    ell = wsdl.getElementsByTagName("element"); // MOZ
for (var i = 0; i < ell.length; i++) {
    if (ell[i].attributes["name"] + "" == "undefined")  // IE
    {
        if (ell[i].attributes.getNamedItem("name") != null && ell[i].attributes.getNamedItem("name").nodeValue == elementname &&  ell[i].attributes.getNamedItem("type") != null)
            return ell[i].attributes.getNamedItem("type").nodeValue;
    }
    else // MOZ
    {
        if (ell[i].attributes["name"] != null && ell[i].attributes["name"].value ==  elementname && ell[i].attributes["type"] != null)
            return ell[i].attributes["type"].value;
    }
}
return "";
}

// private: xmlhttp factory
SOAPClient._getXmlHttp = function () {
try {
    if (window.XDomainRequest) {
        var req = new window.XDomainRequest();
        if (req.readyState == null) {
            req.readyState = 1;
            req.addEventListener("load",
                                function () {
                                    req.readyState  = 4;
                                    if (typeof req.onreadystatechange == "function")
                                        req.onreadystatechange();
                                },
                                false);
        }
        return req;
    }
}
catch (ex) {
    try {
        if (window.XMLHttpRequest) {
            var req = new XMLHttpRequest();
            // some versions of Moz do not support the readyState property and the  onreadystate event so we patch it!
            if (req.readyState == null) {
                req.readyState = 1;
                req.addEventListener("load",
                                function () {
                                    req.readyState  = 4;
                                    if (typeof  req.onreadystatechange == "function")
                                         req.onreadystatechange();
                                },
                                false);
            }
            return req;
        }

    }

    catch (ex) {
        try {
            // Internet Explorer
            if (window.ActiveXObject)
                return new ActiveXObject("Msxml2.XMLHTTP");     //ActiveXObject(SOAPClient._getXmlHttpProgID());
        } catch (e) {
            // Firefox, Opera 8.0+, Safari
            try {

                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                try {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("Your browser does not support AJAX!");
                    return false;
                }
            }
        }


    }
}
}


SOAPClient._getXmlHttpProgID = function () {
if (SOAPClient._getXmlHttpProgID.progid)
    return SOAPClient._getXmlHttpProgID.progid;
var progids = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0",  "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
var o;
for (var i = 0; i < progids.length; i++) {
    try {
        o = new ActiveXObject(progids[i]);
        return SOAPClient._getXmlHttpProgID.progid = progids[i];
    }
    catch (ex) { };
}
throw new Error("Could not find an installed XML parser");
}

提前感谢任何有用的输入。

干杯

The Code (I have also tried without the <code>toString()</code> function) The Web Service XML enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

尝试&#38代替&amp;登录。