[英]jQuery AJAX retrieve SVG from file - ERROR: 'not well-formed'
I have been using many different svg from inkscape and using .load to a specific container element for a while. 一段时间以来,我一直在使用许多与inkscape不同的svg,并使用.load到特定的容器元素。 I recently tried changing the load to a get (AJAX), mainly to be able to prepend the svg. 我最近尝试将负载更改为get(AJAX),主要是为了能够添加svg。
This worked pefectly, svg prepended to element as should do, problem is I get the console error 'not well-formed' when using the ajax get method of retrieving the svg file. 这项工作很完美,svg像应该做的那样放在元素的前面,问题是当我使用ajax get方法检索svg文件时,出现控制台错误“格式不正确”。 I dont like ignoring errors, but this does interfere with the page content. 我不喜欢忽略错误,但这确实会干扰页面内容。
Didnt think it was an issue with the svg, because it was working previoulsy, just to make sure I checked the required namespaces in svg where included(although using inkscape default metadata from standard save). 不要认为这是svg的问题,因为它工作正常,只是为了确保我检查了svg中包含的所需名称空间(尽管使用了标准保存中的inkscape默认元数据)。 eg: 例如:
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ev="http://www.w3.org/2001/xml-events">
Original load Method (No Error): 原始加载方法(无错误):
$('.container-svg').load("img/floorplan/"+ source +".svg", null, function()
{
//other things happen
});
New Ajax Method (Error): 新的Ajax方法(错误):
$.get("img/floorplan/"+ source +".svg").done(function( data )
{
$(".container-svg").prepend($(data).find("svg"));
//other things happen
});
Figured it out.... $.get() is a shorthand version for $.ajax(), except i think it uses XML as default datatype. 弄清楚了...。$ .get()是$ .ajax()的简写版本,但我认为它使用XML作为默认数据类型。 I thought it would use HTML as default so just used longhand and assigned dataType : 'html' this is now compatible with svg. 我认为它将默认使用HTML,因此只使用了longhand并分配了dataType:'html'现在与svg兼容。
$.ajax(
{
url: "img/floorplan/"+ source +".svg" ,
dataType: 'html',
type: 'GET',
success: function(data)
{
$(".container-svg").prepend(data);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.