jQuery AJAX从文件中检索SVG-错误:“格式不正确" [英] jQuery AJAX retrieve SVG from file - ERROR: 'not well-formed'
问题描述
一段时间以来,我一直在使用许多与inkscape不同的svg,并使用.load到特定的容器元素.我最近尝试将负载更改为get(AJAX),主要是为了能够添加svg.
I have been using many different svg from inkscape and using .load to a specific container element for a while. I recently tried changing the load to a get (AJAX), mainly to be able to prepend the svg.
这完全有效,svg像应该做的那样预先放置在element上,问题是使用ajax get方法检索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. I dont like ignoring errors, but this does interfere with the page content.
Didnt认为这是svg的问题,因为它很容易出错,只是为了确保我检查了svg中包含的所需名称空间(尽管使用了标准保存中的inkscape默认元数据).例如:
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). 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">
原始加载方法(无错误):
$('.container-svg').load("img/floorplan/"+ source +".svg", null, function()
{
//other things happen
});
新的Ajax方法(错误):
$.get("img/floorplan/"+ source +".svg").done(function( data )
{
$(".container-svg").prepend($(data).find("svg"));
//other things happen
});
- 这与svg文件中的元数据有关吗?
- 应该/如何纠正错误?
推荐答案
弄清楚了.... $ .get()是$ .ajax()的简写版本,但我认为它使用XML作为默认数据类型.我以为它将默认使用HTML,所以只使用了longhand并分配了dataType:'html'现在与svg兼容.
Figured it out.... $.get() is a shorthand version for $.ajax(), except i think it uses XML as default datatype. I thought it would use HTML as default so just used longhand and assigned dataType : 'html' this is now compatible with svg.
$.ajax(
{
url: "img/floorplan/"+ source +".svg" ,
dataType: 'html',
type: 'GET',
success: function(data)
{
$(".container-svg").prepend(data);
}
});
这篇关于jQuery AJAX从文件中检索SVG-错误:“格式不正确"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!