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:


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 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.


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >

    <script type="text/javascript" src=""></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: '',
                cache: false,
                crossDomain: true,
                dataType: 'html',
                data: { surv: '1170'},
                async: false

            var test = contents;
            test = test.replace(/t/g, ';');
            test = test.replace(/n/g, 'break');
            $('#contentArea').attr('value', test);

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

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


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!