2013-11-22 38 views
2

我有一個PHP應用程序。我需要通過標記搜索instragram圖片。parseError在PHP搜索instagram圖片

Ajax調用的結果是錯誤: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和未捕獲的SyntaxError:意外的標記:」

我希望你能幫助我

非常感謝

+0

您可以在json_encode之前打印$圖像嗎? – deb0rian

+0

你不應該提到你期待json作爲響應的地方嗎? –

+0

@Fratyr是的。 echo $ imagenes Array []。 $ imagenes [0]爲空,結果爲[] – Litox

回答

1

嘗試添加:

dataType:'JSON' 

到$阿賈克斯通話,例如

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

不起作用! – Litox

+0

我已測試過您的代碼,它適用於我,有和沒有具體的數據類型參數的唯一問題是,在你的PHP代碼,你有 回聲$響應; 這是造成問題,你缺少一個}之前 clearTimeout(定時器) 在你的JavaScript –