質問

I have successfully requested to a Basecamp XML file via ajax, but it throws an error (in Google Chrome):

Resource interpreted as Other but transferred with MIME type undefined. Uncaught SyntaxError: Unexpected token <

Firefox give me an error too, but it's a complete different one. I cannot modify the XML file, and the XML file structure is correct. I tried and searched but can find out what's wrong. Any helps would be appreciated.

Here is the jQuery code:

$(document).ready(function() {
    $.ajaxSetup({accepts:'text/xml',cache:false});
    $.ajax({
            type: 'GET',
            dataType: 'jsonp',
            contentType: 'text/xml',
            mimeType: 'text/xml',
            crossDomain: true,
            url: url,
            beforeSend: function(xhr) {
                    var bytes = Crypto.charenc.Binary.stringToBytes(username+":"+password);
                    var base64 = Crypto.util.bytesToBase64(bytes);
                    xhr.overrideMimeType("text/xml;charset=UTF-8");
                    xhr.setRequestHeader("Authorization", "Basic " + base64);
            },
            complete: function(xhr, status) {
                    if (status === 'error' || !xhr.responseText) {
                        $('.result').html('<p><strong>Error:</strong> ' + status + "</p><p><strong>Response Text</strong>:<br /><pre>"+xhr.responseText+"</pre></p>");
                    } else {
                        var data = xhr.responseText;
                        $('.result').html("<pre>"+data+"</pre>");
                    }
            }
            });
    });
役に立ちましたか?

解決

Generally, the problem is that the resource you requested doesn't support JSONP, thus it returns the XML file instead of a script. When you specifies JSONP in the ajax request, the function expects data wrapped in javascript, otherwise it won't work.

Of course you can not request the XML file directly without JSONP because cross-domain access to resources other than script is not allowed in browsers for security concern.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top