September 18, 2012

$.ajax() function working in IE but not in Chrome and FF

Question by icr

I guess this question is not asked in this forum before, tried searching alot but none matched or worked for me. Below is the JSFiddle link of code:

JSFiddle

Everything is working well in IE8 and IE9 but not in any other browsers. I tried looking for the problem in Chrome from which I got

XMLHttpRequest cannot load http://v3.actisku.com/GetUserPostData?pid=Test-37&surv=1170. Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers

I tried searching for necessary solution i.e CORS but couldn’t figure out the solution. I am side by side looking for the same.

EDIT:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<html><head>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            var contents = $.ajax({
                url: 'http://v3.actisku.com/GetUserPostData?pid=Test-37',
                cache: false,
                crossDomain: true,
                dataType: 'html',
                data: { surv: '1170'},
                async: false
            }).responseText;

            var test = contents;
            alert(test);
            test = test.replace(/t/g, ';');
            alert(test);
            test = test.replace(/n/g, 'break');
            alert(test);
            $('#contentArea').attr('value', test);
        });
    </script>
</head>

<body>
<textarea id="contentArea" rows="10" cols="50"></textarea>
</body>
</html>​

Can we manually add headers so that the server feels like it is getting request from IE itself.

Regards,
icr

Answer by Starx

There is nothing in your code, to create Browser incompatibility. In fact they ware working fine in Firefox, and chrome. However, This seems unnecessary.

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}

As mentioned by Mahan, this looks to be like Server Configuration problem.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!