Code

Coding, Programming & Algorithms, Tips, Tweaks & Hacks
Search

Sponsored Ads

Trigerring error() on a cross-domain AJAX request

Opera was the only browser that triggered the error function when an illegal cross-domain AJAX request was being tried via jQuery.

HTML + JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>XDOM</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
    $.ajax(
    {
        url: "http://www.microsoft.com/robots.txt",
        dataType: "text",
        beforeSend: function(XMLHttpRequest)
        {
            $('body').append("sending ... <br/>");
        },
        success: function(data)
        {
            $('body').append("<b>success :</b> data = " + data + "<br/>");
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            $('body').append("<b>error :</b> textStatus = " + textStatus + ", errorThrown = " + errorThrown + "<br/>");
        },
        complete: function(XMLHttpRequest, textStatus)
        {
            $('body').append("<b>complete :</b> textStatus = " + textStatus + "<br/>");
        }
    });
});
</script>
</head>
<body></body>
</html>
HTML + JavaScript

FireFox, Chrome & Safari :

sending ...
success : data =
complete : textStatus = success

Opera :

sending ...
error : textStatus = null, errorThrown = ReferenceError: Security violation
complete : textStatus = undefined

Internet Explorer just didn't fire any of the events - the debugger reported a security violation.

Vanakkam !

0 comments: