简体   繁体   中英

Ajax call on IE 9 (success data - undefined)

When calling specific service with ajax call IE9 returns "undefined":

$.ajax({
    url: URL_PATH,
    type: "get",
    success: function(data) {
        console.log(data);
    }
});

Inspecting same code in Firefox, Chrome, IE10+, it works. I even tried adding:

contentType: "application/json; charset=utf-8",
datatype: "json",

to ajax call properties, but no luck.

Strange thing is that when I call a local JSON file, everything is okay in IE9, but when returning from local server url (information from database) that error occurs. Taking a look at response body, I got well formatted json string.

UPDATE: Also added the error catching block:

error: function(XMLHttpRequest)
    {
        console.log(XMLHttpRequest);
    },

but no luck, it doesn't go inside the error block, it catches "success"

I found a problem. The response header of my rest service was set to charset=UTF8 and the IE couldn't recognize that while other browsers work with no problem, the correct spelling needs to be UTF-8 with a dash :) a rookie mistake. Thanks everyone for suggestions. Closing this thread now.

there are 3 ways to solve this issue,

First one add following lines,

<!--[if lte IE 9]>
<script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.0/jquery.xdomainrequest.min.js'></script>
<![endif]-->

Second one,

solve it by using dataType='jsonp' at the place of dataType='json'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM