简体   繁体   中英

Javascript unexpected token '<' when loading XML from model

I am trying to implement a solution to display XML data on a web page. I'm making use of an ASP.NET MVC Razor view. Testing the application out I used the following:

<script type="text/javascript">
    LoadXMLString('XMLHolder', '<Rfq><IsAbove30k>true</IsAbove30k><ReferenceNumber>ReferenceNumber1</ReferenceNumber><ContactPersonName>ContactPersonName1</ContactPersonName><ContactPersonTelephoneNumber>ContactPersonTe1</ContactPersonTelephoneNumber><ContactPersonCellPhone>ContactPersonCe1</ContactPersonCellPhone><BuyerName>BuyerName1</BuyerName><BuyerTelephoneNumber>BuyerTelephoneN1</BuyerTelephoneNumber><BuyerEmailAddress>BuyerEmailAddress1</BuyerEmailAddress><ProcurementItem><Title>Title1</Title><ClosingDate>1900-01-01T01:01:01+02:00</ClosingDate><Description>Description1</Description><CaptureDate>1900-01-01T01:01:01+02:00</CaptureDate></ProcurementItem><RfqGood><DeliveryTo>DeliveryTo1</DeliveryTo><DeliveryAddress>DeliveryAddress1</DeliveryAddress><DeliverySuburb>DeliverySuburb1</DeliverySuburb><DeliveryPostalCode>1</DeliveryPostalCode><SubmissionFax>SubmissionFax1</SubmissionFax><Specification>Specification1</Specification><SubmissionFax2>SubmissionFax21</SubmissionFax2><DeliveryDate>1900-01-01T01:01:01+02:00</DeliveryDate><Good>Tools &amp; Machinery</Good></RfqGood></Rfq>')

This worked fine. The XML data I'm trying to display is saved as XML in my application database. It is passed to the view in the model. Hence, I tried the following:

<script type="text/javascript">
    LoadXMLString('RfqXmlData', @Html.Raw(Model.RfqXmlData))

At this stage I received the error:

Uncaught SyntaxError: Unexpected token <

I did notice when view the source of the web page that the XML data had linebreaks between the nodes:

XmlDataLineBreaks

So, naturally I tried removing the linebreaks:

<script type="text/javascript">
    LoadXMLString('RfqXmlData', @Html.Raw(Model.RfqXmlData.Replace(System.Environment.NewLine, ""))

This succeeded in removing the linebreaks, but I still got the same error:

XmlDataNoLineBreaks

I have not idea why this is not working, as it did work when I loaded the xml string manually in the JavaScript.

A string in javascript is enclosed in '' or in "", but the example you gave as neither. If you are sure that quotation marks will not be part of the output, enclose @Html.Raw(...) with quotation marks. eg. LoadXMLString('RfqXmlData', '@Html.Raw(...)');

Use the one without line breaks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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