简体   繁体   English

jQuery / Ajax-尝试通过Ajax创建POST方法并获取对HTML的响应

[英]jQuery/Ajax - Trying to create a POST method by Ajax and get response to HTML

Guys i'm trying to create a simple HTML post method by Ajax. 伙计们,我正在尝试通过Ajax创建一个简单的HTML post方法。

Here is my code: 这是我的代码:

<?PHP
 function callInstagram($url)
    {
    $ch = curl_init();
    curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 2
    ));

    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
    }

    $tag = Mage::getStoreConfig('vivastags/vivasgroup/instagra_tag');
    $client_id = "1e0f576fbdb44e299924a93cace24507";
    $Next_URL = $_GET["nexturl"];
    if($Next_URL == ""){
    $url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id.'&count=24';
    } else {
    $url =  $Next_URL;
    }
    $inst_stream = callInstagram($url);
    $results = json_decode($inst_stream, true);
    $maxid = $results['pagination']['next_max_id'];
    $nexturl = $results['pagination']['next_url'];
    //Now parse through the $results array to display your results... 
    ?>
    <div class="holder">
    <?PHP
    foreach($results['data'] as $item){
        $image_link = $item['images']['thumbnail']['url'];
        $Profile_name = $item['user']['username'];
        $PostID = $item['id'];

        echo '<div style="display:block;float:left;">'.$Profile_name.' <br> <img src="'.$image_link.'" /></div>';
    }
    $nextUrlEncoded = urlencode($nexturl);
    ?>
    <div id="LoadedResults"></div>
    </div>
    <?PHP
    $nextUrlEncoded = urlencode($nexturl);
    ?>
    <button type="button" id="LoadMore">Load more Images!</button>

      <script>
    jQuery(document).ready(function($) {
      jQuery('#LoadMore').click(function() {

        var nextUrl   = "<?PHP echo $nextUrlEncoded;?>";

        $.ajax({
          url: 'ajax.php',
          type: 'POST',
          dataType: 'html',
          data: {
            next_url: nextUrl
          },
        }).done(function ( data ) {
          $('#LoadedResults').append(data);
        });

        alert("Post sended!");
      });
    });
  </script>

Note that i have included jQuery1.9.1 in my head tags. 请注意,我在head标签中包含了jQuery1.9.1。

Here is my ajax.php: 这是我的ajax.php:

<?PHP
 function callInstagram($url)
    {
    $ch = curl_init();
    curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 2
    ));

    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
    }

    $tag = "bulgaria";
    $client_id = "1e0f576fbdb44e299924a93cace24507";
    $Next_URL = $_POST["next_url"];
    $url =  $Next_URL;

    $inst_stream = callInstagram($url);
    $results = json_decode($inst_stream, true);
    $maxid = $results['pagination']['next_max_id'];
    $nexturl = $results['pagination']['next_url'];
    //Now parse through the $results array to display your results... 
    foreach($results['data'] as $item){
        $image_link = $item['images']['thumbnail']['url'];
        $Profile_name = $item['user']['username'];

        echo '<div style="display:block;float:left;">'.$Profile_name.' <br> <img src="'.$image_link.'" /></div>';
    }

The problem is that there is no response, when i click the button just nothing happens. 问题是没有响应,当我单击按钮时什么也没发生。

Where is my mistake and how i can make this work? 我的错误在哪里以及如何进行这项工作?

Note, in your ajax, you have type: 'POST' . 注意,在您的ajax中,您type: 'POST' So your PHP should be looking for "POST" properties: 因此,您的PHP应该在寻找“ POST”属性:

$_GET["next_url"];

Should be 应该

$_POST["next_url"];

Also, 也,

You need to put quotes around this: 您需要在这周围加上引号:

var nextUrl   = "<?PHP echo $nextUrlEncoded;?>";

Finally, if this still doesn't work, I would open up developer tools in Chrome and check out the Network tab. 最后,如果仍然无法解决问题,我将在Chrome中打开开发人员工具,然后查看“网络”标签。 Make sure that your XHR is sending the right data. 确保您的XHR发送正确的数据。 I'm curious if $nextUrlEncoded is never getting set correctly. 我很好奇$nextUrlEncoded是否从未正确设置。 Then make sure that the response from your XHR includes information. 然后,确保XHR的响应中包含信息。 This will help you determine where the break is coming from. 这将帮助您确定中断的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM