本文介绍了parseError在PHP搜索Instagram的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP应用程序。我需要寻找instragram图片按标签。

I have a php app. I need search instragram pictures by tag.

Ajax调用的结果是错误:parseError

The result of ajax call is 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和未捕获的SyntaxError:意外的标记:

I need to search instagram pics by tag. But ajax request return parseError and Uncaught SyntaxError: Unexpected token :"

我希望你能帮助我。

多谢

推荐答案

试着加入:

dataType:'JSON'

您$就调用,例如。

to your $.ajax call, e.g.

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

这篇关于parseError在PHP搜索Instagram的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 14:14