JQuery.ajax () - undefined data returned in IE9

I have a very simple code:

$.ajax({ cache: false, dataType: 'html', complete: function(jqXHR){ console.log(jqXHR.responseText); }, success: function(data){ console.log(data); }, url: 'http://follows.pl/pages/ajaxtest' }); 

it returns some text in ff, chrome and IE8, but in IE9 it shows twice "undefined".

I looked at the developer tool in IE9 and it shows a normal answer, so the request works fine, the answer is fine, but the variables are undefined

response headers:

 Response HTTP/1.1 200 OK Cache-Control no-cache Content-Type text/html; charset: UTF-8 Pragma no-cache 

answer

 string(4) "test" 
+8
jquery ajax internet-explorer-9
source share
3 answers

I suspect this is your problem:

 Content-Type text/html; charset: UTF-8 

This value is incorrectly formatted (":" after the encoding is incorrect), and he does not like IE9, but was silent, instead of saying something useful. Try the following:

 Content-Type: text/html;charset=utf-8 
+7
source share

I tried everything to solve this problem of ajax posting in the IE browser (for example, adding an ajax object in jQuery without a cache, dataType, configType, etc.), but in the end the problem was not in ajax / javascript, but to the file PHP: for IE browser only, the PHP file should start with the following header :

 header("Content-type: text/html; charset=utf-8"); 

therefore, you must explicitly specify the type of content of the php page that you will get as a result of your ajax call.

An example assuming an html page called one.html where you place your javascript and php page called two.php

In one.html set javascript as

 var url = 'two.php'; $.ajax({ url: url, type: "POST", success: function(response){ alert(response) } }); 

On the two.php page, set the following:

 <?php header("Content-type: text/html; charset=utf-8"); echo ('stuff to do'); ?> 

so for me it worked like a charm!

+1
source share

try the following:

 $.ajax({ cache: false, dataType: 'html', complete: function(data){ console.log(data); }, success: function(data){ console.log(data); }, url: 'http://follows.pl/pages/ajaxtest' }); 

notifications

in function of success

  success: function (data, textStatus, jqXHR) 

the object is an argument of third .

you are really responding to data by accessing a property that does not exist there.

also in full function

  complete: function (jqXHR, complete_textStatus) 

here is the first place object!

you need to remember the places.

0
source share

All Articles