views:

163

answers:

1

I am trying to communicate with Google's spell check service using jQuery. Google's service requires that you post XML and it will in turn return an XML response. In IE, the success callback gets hit every time, but in non-IE browsers (tested in Firefox and Chrome) the error callback is getting hit every time.

The biggest difference is in how the XML that is posted is created. For IE, an ActiveX object is getting created; for all other browsers the DOMParser is used (example from w3schools).

Below is my test code (NOTE: 'spell-check' represents the ID of an HTML button.) What am I missing or need to change to successfully post XML from jQuery across browsers?

<script type="text/javascript">

var xmlString = '<?xml version="1.0"?><spellrequest><text>mispell</text></spellrequest>';

function createXMLDocument(s) {
    var xmlDoc;
    if (window.DOMParser) {
        var parser = new DOMParser();
        xmlDoc = parser.parseFromString(s, 'text/xml');
    } else {
        xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
        xmlDoc.async = 'false';
        xmlDoc.loadXML(s);
    }
    return xmlDoc;
}

$(function() {
    $('#spell-check').live('click', function(e) {
        e.preventDefault();
        $.ajax({
            cache: false,
            contentType: 'text/xml',
            data: createXMLDocument(xmlString),
            dataType: 'xml',
            processData: false,
            type: 'POST',
            url: 'https://www.google.com/tbproxy/spell?lang=en',
            success: function(data, textStatus, XMLHttpRequest) {
                alert(textStatus);
                //debugger;
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
                //debugger;
            }
        });
    });
});

A: 
Gaby
It fails without xml tag as well.
300 baud
updated my answer.. because you need to send the xml as a string.. (the activeX version must be handling this behind the scenes.. )
Gaby
I was pretty sure I had tested that way as well, but I went ahead and tested passing it as an escaped string both with and without the beginning xml tag. Still a no-go.
300 baud
you cannot make cross-domain AJAX calls.. have a look at the updated answer with links to why and a common workaround..
Gaby
Ugh. I was trying to avoid having a "proxy" page, but I guess I have no choice. Thanks.
300 baud