奇怪的Ajax / Javascript问题

时间:2016-10-22 23:33:28

标签: javascript php jquery ajax

所以我在ajax调用和返回的数据时遇到了一个奇怪的问题。基本上我正在通过ajax调用我的脚本,然后通过Amazon API获取一些数据。返回数据时,网址似乎打破了实际的返回。这是我传递给ajax的代码,

        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();

        }

在我的PHP代码中,这就是我返回它的方式:

     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;
        }


      }

我从API中挖掘出的亚马逊链接如下所示:

   [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

令人困惑的是,为什么网址中间的某些内容会打破回报。任何建议表示赞赏。谢谢!

1 个答案:

答案 0 :(得分: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();

    }