无效的XML - 在跨域ajax http / get之后

时间:2013-05-10 17:02:13

标签: xml ajax cross-domain

这是我的问题:我正在尝试使用网络T恤设计师,该设计师使用在线T恤网站API。该问题应该与应用程序无关,但它有助于我描述系统。这个设计器用javascript编码,它向一个调用T恤网站API的php代理发出请求。我在网络服务器上将代理和html与脚本一起上传,一切正常。

不幸的是,我需要从其他域加载html。首先,这会引起同源错误。我通过添加到php代理来修复它

header("Access-Control-Allow-Origin: *");

但是,现在出现以下代码

的新错误
this.getShop = function (shopId) {
    var shop = null;
    $.ajax({
        type: "GET",
        async: false,
        cache: true,
        url: this.createUrl(this.baseHttpUrl + "/shops/" + shopId),
        dataType: "xml",
        crossDomain: true,
        success: function(data) {
            shop = $(data).find("shop");
        },
        error:function(xhr,err,other){
            alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\nerror: "+err+"\nother: "+other);
            alert("responseText: "+xhr.responseText);
        }
    });
    return shop;
};

我从第一个提醒得到的是

readyState: 4
status: 200
error: parsererror
other: Error: Invalid XML: data

最终的“数据”是一些XML数据,我从xhr.responseText得到的,这看起来就像T恤网站应该发送的那样,看起来也完全有效......我想这还有一些东西与跨域设置有关。该错误来自parseXML jQuery函数,我相信如果我从同一个Web服务器运行所有内容,甚至都不会调用它...

编辑:这是我得到的数据

的一个例子
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<shop xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net" xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx" id="xxxxxx">
<name>example</name>
<description>example</description>
<type>CLASSIC</type>
<user xlink:href="http://api.spreadshirt.net/api/v1/users/xxxxxxx" id="xxxxxxx"/>
<country xlink:href="http://api.spreadshirt.net/api/v1/countries/6" id="6"/>
<language xlink:href="http://api.spreadshirt.net/api/v1/languages/2" id="2"/>
<currency xlink:href="http://api.spreadshirt.net/api/v1/currencies/1" id="1"/>
<address xlink:href="http://api.spreadshirt.net/api/v1/shops/420067/address"/>
<passwordRestricted>false</passwordRestricted>
<hidden>false</hidden>
<mandator id="1"/>
<shippingUseCase id="1"/>
<defaultShippingType id="1"/>
<discountSupported>false</discountSupported>
<productTypes xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/productTypes"/>
<printTypes xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/printTypes"/>
<fontFamilies xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/fontFamilies"/>
<productTypeDepartments xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/productTypeDepartments"/>
<shippingTypes xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/shippingTypes"/>
<designCategories xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/designCategories"/>
<designs xlink:href="http://api.spreadshirt.net/api/v1/shops/420067/designs"/>
<articleCategories xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/articleCategories"/>
<articles xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/articles"/>
<products xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/products"/>
<applications xlink:href="http://api.spreadshirt.net/api/v1/shops/xxxxxx/applications"/>
<currencies xlink:href="http://api.spreadshirt.net/api/v1/currencies"/>
<languages xlink:href="http://api.spreadshirt.net/api/v1/languages"/>
<countries xlink:href="http://api.spreadshirt.net/api/v1/countries"/>
<baskets xlink:href="http://api.spreadshirt.net/api/v1/baskets"/>
</shop>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

1 个答案:

答案 0 :(得分:0)

服务器端正在向您的XML数据末尾添加<script>标记。如果可能的话,尝试停止正在做这个服务器端的任何事情。

如果无法做到这一点,以下内容应将其删除:

this.getShop = function (shopId) {
    var shop = null;
    $.ajax({
        type: "GET",
        async: false,
        cache: true,
        url: this.createUrl(this.baseHttpUrl + "/shops/" + shopId),
        dataType: "text",
        crossDomain: true,
        success: function(data) {
            // rough parsing of data
            shop = $.parseXML(data.split("\<\!-- Hosting 24")[0]);
        },
        error:function(xhr,err,other){
            alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\nerror: "+err+"\nother: "+other);
            alert("responseText: "+xhr.responseText);
        }
    });
    return shop;
};