JavaScript意外令牌'<'从模型加载XML时 [英] Javascript unexpected token '<' when loading XML from model

查看:49
本文介绍了JavaScript意外令牌'<'从模型加载XML时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施解决方案以显示网页上的XML数据.我正在使用ASP.NET MVC Razor视图.测试该应用程序时,我使用了以下内容:

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>')

这很好.我要显示的XML数据已另存为XML在我的应用程序数据库中.它被传递到模型中的视图.因此,我尝试了以下方法:

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:

未捕获到的SyntaxError:意外令牌<

Uncaught SyntaxError: Unexpected token <

当查看网页源时,我确实注意到XML数据在节点之间有换行符:

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

因此,自然地,我尝试了删除换行符:

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:

我不知道为什么它不起作用,因为当我在JavaScript中手动加载xml字符串时它确实起作用了.

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

推荐答案

"或"中都包含了javascript中的字符串,但您给出的示例都不是.如果您确定引号不会成为输出的一部分,请用引号将@ Html.Raw(...)括起来.例如.LoadXMLString('RfqXmlData','@ Html.Raw(...)');

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(...)');

使用没有换行符的

这篇关于JavaScript意外令牌'&lt;'从模型加载XML时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆