2017-10-26 108 views
0

AJAXでCookieセットを取得しようとしています。これは、Androidデバイス、Chrome、FF、Opera、Microsoft Edge、Safari、iOSデバイスを除くIE上で動作します。 AJAXが袖を隠しているSafariとiOSに関する不足していることがありますか?SafariとiOSデバイスがjQuery AJAXコールで動作しない

ここでは、Androidデバイスなどで動作するコードを示します。何らかの理由で

のJavascriptファイル

function setCookie() { 
var $cookieName = 'example-cookie', 
      $cookieMethod = 'example-method', 
      $cookieGet = 'example-get' 

     $.ajax({ 
      type: 'POST', 
      url: $(location).attr('protocol') + 'to/location.php', 
      data: { 
       name: $cookieName, 
       method: $cookieMethod, 
       get: $cookieGet 
      }, 
      cache: false, 
      success: function(data, textStatus, jQxhr) { 
       try { 
        $('.report .display-result').css('display', 'none').css('position', 'absolute') 
         .css('width', '100%').html($(data).filter('.display').html()).fadeIn(1000); 

        setTimeout(function() { 
         $('.report .display-result').fadeOut(1000); 
        }, 4000); 
       } catch (err) {/*alert(err);*/} 
      }, 
      error: function(jqXhr, textStatus, errorThrown) { 
       try { 
        alert(jqXhr.responseText); 
       } catch (err) {/*alert.log(err);*/} 
      } 
     }); 
    } 

PHPファイル

<?php 

require_once '../to/class/Class.php'; 

$uc   = new Class(); 
$cookieName = $_POST['name']; 
$cookieMethod = $_POST['method']; 
$cookieId  = $_POST['get']; 

$uc->method($_POST['name'], $_POST['method'], $_POST['get']); 
?> 

<?php 
if (!empty($uc->method($_POST['name'], 'get-cookie'))): 
    $cookie = json_decode($uc->method($_POST['name'], 'get-cookie')); 

    // Making sure the cookie exists and is not empty. 
    if (!empty($cookie)): 
     // Making sure the cookie has id's, if not, will spit out 'Successly added to report'. 
     if (isset($cookie->data->id)) { 
      foreach ($cookie->data->id as $chkDuplicates) { 
       // If any id's match, there is a duplicate. Make sure that they know they've added this already. 
       if ($chkDuplicates == $_POST['get']) { 
        $duplicateFound = true; 
        break; 
       } 
      } 
     } 

     if (isset($duplicateFound)): ?> 
      <div class="display info"> 
       <h3 class="alert alert-info">You've already added this to your report.</h3> 
      </div> 
     <?php else: ?> 
      <div class="display success"> 
       <h3 class="alert alert-success">Successfully added to report.</h3> 
      </div> 
     <?php endif; ?> 
    <?php endif; ?> 
<?php else: ?> 
    <div class="display failure"> 
     <h3 class="alert alert-failure">Unfortunately, that item wasn't added, please refresh the page and try again.</h3> 
    </div> 
<?php endif; ?> 

これはSafariとのiOSデバイスでは動作しないようです。アラート(jqXhr)を取得しようとすると、何も得られません。 jqXhr.responseTextと同じですが、textStatusは私に「エラー」を返し、errorThrownは残りのものと同様に空白です。私はこれに少し迷っています。ここでのサポートは非​​常に高く評価されており、これを見ていただきありがとうございます。

答えて

0

Safariは、AJAXリクエストを多量にキャッシュすることができます。おそらく、以前はSafariのjQueryをテストしていましたが、応答が完全に機能し、APIが空の応答を出していたのかもしれません。

これを試して、Safariキャッシュを無効にして、API URL + '&nocache=' + new Date().getTime()の変更を行ってください。これは基本的にSafariがすべてのリクエストで異なるURLのようにAjax URLを扱うようにするため、SafariはAPIエンドポイントから新鮮な応答を得るでしょう。

$.ajax({ 
      type: 'POST', 
      url: $(location).attr('protocol') + 'to/location.php' + '&nocache=' + new Date().getTime(), 
      data: { 
       name: $cookieName, 
       method: $cookieMethod, 
       get: $cookieGet 
      }, 
      cache: false, 
      success: function(data, textStatus, jQxhr) { 
       try { 
        $('.report .display-result').css('display', 'none').css('position', 'absolute') 
         .css('width', '100%').html($(data).filter('.display').html()).fadeIn(1000); 

        setTimeout(function() { 
         $('.report .display-result').fadeOut(1000); 
        }, 4000); 
       } catch (err) {/*alert(err);*/} 
      }, 
      error: function(jqXhr, textStatus, errorThrown) { 
       try { 
        alert(jqXhr.responseText); 
       } catch (err) {/*alert.log(err);*/} 
      } 
     }); 
+0

その上に運がありません。私はSafariがそうする傾向があることを見てきました。キャッシュ:falseはそれについてもトリックを行うだろうと考えています。応答していただきありがとうございます。 –

0

いいので、JSファイルとAJAXのURLと関係がありました。

どうやら、iOS版/ Safariが

$(location).attr('protocol') 

を読んでいないので、

$(location).attr('origin') 

ったく、追跡があまりにも時間がかかったような単純なものにそれを変更してください。これが将来誰にでも役立つことを願っています。

関連する問題