php搜索instagram图片中的parseError

时间:2013-11-22 15:33:16

标签: php jquery ajax json instagram

我有一个php应用程序。我需要通过标签搜索instragram图片。

ajax调用的结果是Error:parseError

这是来源:

的index.html

<!doctype html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Instagram Photo Instant Search App with jQuery</title>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script type="text/javascript" src="ajax.js"></script>
</head>

<body>
    <div id="w">
        <section id="sform">
            <small>Note: No spaces or punctuation allowed. Searches are limited to one(1) keyword.</small>
            <input type="text" id="s" name="s" class="sfield" placeholder="Enter a search tag..." autocomplete="off">
        </section>

        <section id="photos"></section>
    </div>
</body>
</html>

ajax.js

$(document).ready(function(){   
var sfield = $("#s");   
var container =$("#photos");    
var timer;      
function instaSearch() {
$(sfield).addClass("loading");      
$(container).empty();       
var q =$(sfield).val();

$.ajax({            
type: 'POST',           
url: 'instasearch.php',                                 
data: "q="+q,           
success: function(data){
$(sfield).removeClass("loading");               
$.each(data, function(i, item) {
var ncode = '<div class="p"><a href="'+data[i].src+'" class="fullsize" target="_blank"><img src="img/full-image.png" alt="fullsize"></a> <a href="'+data[i].url+'" target="_blank"><img src="'+data[i].thumb+'"></a></div>';

$(container).append(ncode);
});
},          
error: function(xhr, type, exception) { 
alert(exception);
alert(xhr.responseText);    
                                alert(type);
                                if (status === 'error' || !xhr.responseText) 
                                {
                                   alert("podrido");
                                   handleError();
                             }                            
        $(sfield).removeClass("loading");
$(container).html("Error: " + type);            }       });     }       /**      * keycode glossary      * 32 = SPACE    * 188 = COMMA   * 189 = DASH    *
190 = PERIOD     * 191 = BACKSLASH   * 13 = ENTER    * 219 = LEFT BRACKET
 * 220 = FORWARD SLASH   * 221 = RIGHT BRACKET   */
$(sfield).keydown(function(e){
 if(e.keyCode == '32' || e.keyCode == '188' || e.keyCode == '189' || e.keyCode == '13' || e.keyCode == '190' || e.keyCode == '219' ||
 e.keyCode == '221' || e.keyCode == '191' || e.keyCode == '220') {
 e.preventDefault();
 else {
clearTimeout(timer);

timer = setTimeout(function() {
 instaSearch();             }, 900);   
}
});

});

instasearch.php

<?php
header('Content-type: application/json');

$client = "d10b95cf56094bca8b841734baadc367";
$query = $_POST['q'];
$clnum = mt_rand(1,3);

$api = "https://api.instagram.com/v1/tags/".$query."/media/recent?client_id=".$client;


function get_curl($url) {
    if(function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );        
        $output = curl_exec($ch);
        echo curl_error($ch);
        curl_close($ch);
        return $output;
    } else{
        return file_get_contents($url);
    }
}

$response = get_curl($api);
echo $response;
$images = array();

if($response){
    foreach(json_decode($response)->data as $item){     
        $src = $item->images->standard_resolution->url;
        $thumb = $item->images->thumbnail->url;
        $url = $item->link;

        $images[] = array(
        "src" => htmlspecialchars($src),
        "thumb" => htmlspecialchars($thumb),
        "url" => htmlspecialchars($url)
        );

    }
}

print_r(str_replace('\\/', '/', json_encode($images)));
die();
?>

我需要通过标签搜索instagram图片。但是ajax请求返回parseError和Uncaught SyntaxError:意外的令牌:“

我希望你能帮助我

非常感谢

1 个答案:

答案 0 :(得分:1)

尝试添加:

dataType:'JSON'

到$ .ajax电话,例如

$.ajax({            
dataType:'JSON',
type: 'POST',
...