2016-10-22 9 views
0

私は、ajax呼び出しと返されるデータで起きる変な問題があります。基本的には私のスクリプトにajax経由で電話をかけています。このスクリプトはAmazon API経由でデータを取得しています。データが返されると、URLが実際のリターン自体を破っているように見えます。私はAPIの掘る奇妙なAjax/Javascriptの問題

 if($title != ''){ 
     if($d < 2){ 

       $ret .= '<td width="50%" style="padding-left:20px;">'; 
       $ret .= '<input type="checkbox" name="selection[]" value="' . $i . '"> Select This Item'; 
       $ret .= "<h5 class=\"media-heading\"><a href=\"" . $text_link . "\" target=\"_blank\" class=\"title-color\">" . $result[$i]['title'] . "</a></h5>"; 

       $ret .= '<div class="avatar avatar-xl avatar-circle"><img src="' . $image . '"></div>'; 
       $ret .= "</td>";         

       $d++; 
     } 
     else{ 
      $ret .= "</tr>"; 
      $ret .= "<tr>"; 

      $d = 0; 
     } 


     } 

アマゾンのリンクは次のようになります:ここに私のPHPコードで

 if(flag != false){ 

     var myvars = {"keywords": keyword, "aff_code": aff_code, "roundup_title": roundup_title, "user_id": user_id, "newtab": newtab, "nofollow": nofollow}; 


      $.ajax({ 
      url: 'process.php', 
      type: "POST", 
      data: myvars, 
      dataType: 'json', 
       xhrFields: { 
        onprogress: function(e) 
        { 
       var this_response, response = e.currentTarget.response; 
       if(last_response_len === false) 
       { 
       this_response = response; 
       last_response_len = response.length; 
       } 
       else 
       { 
       this_response = response.substring(last_response_len); 
       last_response_len = response.length; 
       } 
       //console.log(this_response); 
       $("#result").html(this_response).slideDown(); 
        } 
       } 
      }) 





      .done(function(data) 
      { 
       //console.log('Complete response = ' + data); 
       var doned = "That wasn't so hard!"; 
       $("#result").html(doned).slideDown(); 
      }) 
      .fail(function(data) 
      { 
       console.log('Error: ', data); 
      }); 
      //$("#result").hide().html(output).slideDown(); 
      $("#result").html("Just a moment...").slideDown(); 

     } 

、AJAXに渡す私のコードですが、これは私がそれを返すい方法です

[amazon_link] => Array 
      (
       [0] => http://www.amazon.com/Anaheim-Ducks-Mens-Patriotic-Snapback/dp/B01EI53EA0%3Fpsc%3D1%26SubscriptionId%3DAKIAIA66NHFH2G5I4XIA%26tag%3Dtest-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB01EI53EA0 
      ) 

しかし、ここに私の問題があります。結果は次のようになりますよう時々、私はすべて一緒に、URLとコードの残りの部分を壊してしまう結果を得る:

%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB01D8YMIJK" target="_blank" class="title-color">Just Play Kitty Surprise Plush, Cleo 
それは、URLの途中で何かがリターンを破る理由として私を不可解だ

。どんな提案も感謝しています。ありがとう!

答えて

0

リンクがサーバーから送信または受信されたときにリンクで許可されない文字があります。メインは&です。 あなたがbase64にすることは、phpファイル(phpのbase64_encode関数を使用)でデータをエンコードし、window.atob関数を使用してブラウザでデコードすることです。

//javascript 
    $.ajax({ 
     url: 'process.php', 
     type: "POST", 
     data: myvars, 
     dataType: 'json', 
      xhrFields: { 
       onprogress: function(e) 
       { 
      var this_response, response = e.currentTarget.response; 
      if(last_response_len === false) 
      { 
      this_response = response; 
      last_response_len = response.length; 
      } 
      else 
      { 
      this_response = response.substring(last_response_len); 
      last_response_len = response.length; 
      } 
      //console.log(this_response); 
      $("#result").html(this_response).slideDown(); 
       } 
      } 
     }) 





     .done(function(data) 
     { 
     data = window.atob(data); //decoding data 
      //console.log('Complete response = ' + data); 
      var doned = "That wasn't so hard!"; 
      $("#result").html(doned).slideDown(); 
     }) 
     .fail(function(data) 
     { 
      data = window.atob(data) //decoding result 
      console.log('Error: ', data); 
     }); 
     //$("#result").hide().html(output).slideDown(); 
     $("#result").html("Just a moment...").slideDown(); 

    }