1

Here is my problem: I am trying to use a web t-shirt designer that uses an online t-shirt site API. The issue should have nothing to do with the application but it helps me describing the system. This designer is coded in javascript, it makes requests to a php proxy which calls the t-shirt site APIs. I uploaded the proxy and the html with the script on a webserver and everything works as it should.

Unfortunately, I need to load the html from a different domain. At first, this would rise a same-origin error. I fixed it by adding to the php proxy a

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

However, now a new error arises from the following code

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;
};

What I get from the first alert is

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

Where the final "data" is some XML data, the same I get from xhr.responseText, that seems exactly what the t-shirt site is supposed to send and also seems perfectly valid... I suppose this has still something to do with the cross-domain setting. The error comes from the parseXML jQuery function which I believe is not even invoked if I run everything from the same webserver...

Edit: Here is an example of what I get as data

<?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 -->
jaykay
  • 13
  • 4
  • 1
    If you specify `dataType: "xml"`, JQuery will try to parse the data returned as XML. Are you sure it is XML that is returned? Please post an example. You could always specify `dataType: "text"` to stop JQuery parsing it. – mccannf May 10 '13 at 19:45
  • I added an example, I think I cannot set it as text because of the manipulation I need to do afterwards – jaykay May 11 '13 at 11:38
  • What manipulation do you mean? If you get the response as text you can parse it. http://stackoverflow.com/questions/649614/xml-parsing-of-a-variable-string-in-javascript – Paul Grime May 11 '13 at 12:35
  • After I wrote that I realized I could, I haven't tried yet thought, I'll report any result. However, I still don't understand why it works perfectly if the call is not cross-domain – jaykay May 11 '13 at 12:37
  • Is the text after the end tag `` in the data? This is what is invalidating the XML. Something appears to be appending it to the XML server side. – mccannf May 11 '13 at 12:39

1 Answers1

0

Something server-side is adding <script> tags to the end of your XML data. If possible, try to stop whatever is doing this server-side.

If this is not possible, the following should strip this off:

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;
};
mccannf
  • 16,619
  • 3
  • 51
  • 63
  • This question talks about how to disable the addition of analytics javascript to XML in PHP: http://stackoverflow.com/questions/9050433/disable-statistics-analytic-javascript – mccannf May 11 '13 at 12:57
  • I don't believe this solution would work because the script goes into the error branch (and I suppose it skips the success part). However, adding an `exit();` statement at the end of proxy.php (as suggested in the other question) seems to strip it off just fine and everything works! so thank you! I'm still curious why the xml does not get parsed if the request is from the same domain and I only get a browser console warning for the script part: `Resource interpreted as Script but transferred with MIME type text/html: "http://stats.hosting24.com/count.php".` – jaykay May 11 '13 at 13:29