JSON 応答を使用して、異なるブラウザー間で奇妙な動作をしていることに気付きました。私が問題を抱えているブラウザは、Safari Mobile、特に iOS シミュレータです。
私は Codeigniter を使用しており、部分的なビューを返しています。すべてのブラウザー (Safari Mobile を除く) で、JSON 応答は次を返します。
response.Html =
"<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>"
これはまさに私が望んでいるものです.6つのリストアイテムが必要で、これをページに挿入します. しかし、Safari Mobile では、何らかの理由で、応答に 5 つのオブジェクトの配列が含まれています。これらのオブジェクトには、部分ビューを作成するために使用する必要があるデータが含まれています。
response: Array[5]
0: Object
1: Object
2: Object
3: Object
4: Object
length: 5
このための AJAX 呼び出しは次のようになります。
function getRandomMovies () {
$.mobile.showPageLoadingMsg("a", "Fetching random movies");
$.ajax({
url: "random/get_random_movies",
type: "POST",
dataType: "json",
contentType: "application/json",
cache: false,
success: function (response) {
$('#random-gallery').html(response.html);
initPhotoSwipe();
$.mobile.hidePageLoadingMsg();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
}
AJAX 呼び出しのコントローラー メソッドには、HTML を返す必要があり、ほとんどのブラウザーで返される次のものがあります。
public function get_random_movies(){
$movies = $this->random_model->get_random_movies();
$data['results'] = $movies;
$response['html'] = $this->load->view('partials/_random_movie_multiple', $data, true);
echo json_encode($response);
}
これらすべてのコード ブロックを貼り付けたことを許していただく必要がありますが、問題を適切にデバッグするためにはすべて必要だと思います。この問題のデバッグに膨大な時間を費やしましたが、役に立たなかったので、これに関する助けをいただければ幸いです。
編集1:
_random_movie_multiple.php
foreach ($results as $result) {
echo '<li><a data-ajax="false" rel="external" data-href="search/movie/'.url_title($result->title, "-", true).'/'.$result->id.'" data-caption="'.$result->title.' ('.$result->release_year.')" href="'.$result->img284.'"><img src="'.$result->img88.'"/></a></li>';
}