Ajaxで動いたりエラーだったり動かない不具合iOS、Safari、https

Ajaxでphpを呼び出して~の処理でハマった

  • PCやAndroidだと動くのに、iOSとかiPhoneだと動いたり動かなかったりする不具合。
  • 特に最初に開いたときは動いて、2回目以降はエラーが出るとき。
  • エラー戻り値は「undefined」とか「0」
  • サイトはhttps

解決方法

async:falseとbeforeSend:を入れたらOKでした。

URLの指定も//からにするのもポイント。httpとかhttpsとか入れないほうがいい。

$.ajax({
	async:false,
	type:'post',
	cache:false,
	url: '//~~~/~~~.php',
	data:{
		send_data:in_send_data,
		flg:0
	},
	beforeSend:function(xhr){
	  if (window.navigator.userAgent.toLowerCase().indexOf('safari') != -1)
	    xhr.setRequestHeader("If-Modified-Since", new Date().toUTCString());
	},
	success: function(data){
		alert("OK");
	},
    error: function(XMLHttpRequest, textStatus, errorThrown) {
    	alert("ERROR " + XMLHttpRequest.status + "-----" + textStatus + "------" + errorThrown.message + "---" + location.pathname);
	}
});
	

 

参考までにエラーが出ていたソースはこんな感じ

$.ajax({
	type:'post',
	cache:false,
	url: '//~~~/~~~.php',
	data:{
		send_data:in_send_data,
		flg:0
	},
	success: function(data){
		alert("OK");
	},
    error: function(XMLHttpRequest, textStatus, errorThrown) {
    	alert("ERROR " + XMLHttpRequest.status + "-----" + textStatus + "------" + errorThrown.message + "---" + location.pathname);
	}
});

 

お客様を何日待たせてしまったんだよぉっ!(><)

どうか世界からsafariとIEが××されますように…(>人<)

 

コメントを残す