1

这是我的问题:我正在尝试使用使用在线 T 恤网站 API 的网络 T 恤设计师。这个问题应该与应用程序无关,但它有助于我描述系统。这个设计器是用 javascript 编码的,它向调用 t-shirt 站点 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 函数,如果我从同一个网络服务器运行所有内容,我相信它甚至不会被调用......

编辑:这是我作为数据获得的示例

<?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 -->
4

1 回答 1

0

服务器<script>端正在向 XML 数据的末尾添加标签。如果可能,请尝试停止正在执行此服务器端的任何操作。

如果这是不可能的,下面应该去掉它:

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;
};
于 2013-05-11T12:44:20.213 回答