2014-03-31 31 views
0

我想運行的一切在一個給定標題名稱例如星球大戰這裏這個搜索http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=star%20wars設置本地代理來訪問IMDB

我想列出一個表格式的所有結果像搜索

這裏是代碼,我到目前爲止,我已經不得不從使用容易omdb API改變,因爲這將只允許最多十個結果

現在我不斷收到JavaScript錯誤任何幫助PLZ我所知,我需要設置本地代理需要幫助PLZ

很想例子

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <title>Sample</title> 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
    <script> 
    $(document).ready(function() { 
     $("#SampleSearchButton").click(function() { 
      getImdbInfo($("#title").val()); 
     }) 
    }); 

    // The function below takes the entered title and searchs imdb for a match then it displays as followed 

    function getImdbInfo(Title) { 
     $.ajax({ 
      url: "http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=" + Title, 
      cache: false, 
      dataType: "json", 
      success: function(data) { 
       // you get an object for iteration, the keys are Title, Type, Year, imdbID 
       console.log(data); 

       var str = '<table>'; 
       str += "<thead><th>Index</th><th>Title</th><th>Type</th><th>Year</th><th>imdbID</th></thead>" 

       // iterate over the data result set 
       $.each(data.Search, function(index, element) { 
        str += "<tr>"; 
        str += "<td>" + index + "</td>"; 
        str += "<td>" + element.Title + "</td>"; 
        str += "<td>" + element.Type + "</td>"; 
        str += "<td>" + element.Year + "</td>"; 
        str += "<td>" + element.imdbID + "</td>"; 
        str += "</tr>"; 
       }); 

       str += '</table>'; 

       // insert the html 
       $("#SampleResults").html(str); 
      }, 
      error: function (request, status, error) { alert(status + ", " + error); } 
     }); 
    } 
    </script> 
</head> 
<body> 


<!-- search textbox --> 
<input type="text" id="title" placeholder="Enter Name for search"> 

<!-- do search button --> 
<button type="text" id="SampleSearchButton">Search</button> 

<!-- display results container --> 
<div id="SampleResults"></div> 
</body> 
</html> 
+0

這裏是一個小提琴http://jsfiddle.net/Tv53v/但具有錯誤'的XMLHttpRequest無法加載http://www.imdb.com/XML /找到?JSON = 1&NR = 1&TT =&上q = fred的&_ = 1396227974662。請求的資源上沒有「Access-Control-Allow-Origin」標題。 Origin'http://fiddle.jshell.net'因此是不允許訪問的。「# –

+0

從http://stackoverflow.com/questions/1966503/does-imdb-provide-an-api來:」缺點:沒有JSONP。爲了從跨域使用JavaScript,需要本地代理。「 –

+0

任何人都可以告訴我如何做到這一點使用本地代理或步驟 – Jamiex304

回答

0
您輸入標題
  • 標題被附加到URL,它調用本地PHP文件
  • 本地PHP文件接受標題和重視它的API
    • URL您要撥打
    • 的請求時,返回的內容
    • 返回的內容,然後通過JS接受
    • 檢查console.log的精確數據結構
    • 到主鍵「title_popular」和「title_exact」,這就是爲什麼有兩個表
    • 注意「description」和「title_description」,兩者似乎是相同的API BUG?),所以它們被打印兩次!
    • 我沒有構建表完全
    • 也許你應該問別人的時候,如何打印多對象更優雅

    PHP

    IMDB-fetcher.php

    <?php 
    $title = $_GET['title']; // <- you need to secure this 
    echo file_get_contents(
        'http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=' . $title 
    ); 
    

    HTML

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
        <title>Sample</title> 
    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
        <script> 
        $(document).ready(function() { 
         $("#SampleSearchButton").click(function() { 
          getImdbInfo($("#title").val()); 
         }) 
        }); 
    
        // The function below takes the entered title and searchs imdb for a match then it displays as followed 
    
        function getImdbInfo(title) { 
         $.ajax({ 
          type: 'GET', 
          url: "imdb-fetcher.php?title=" + title, // <-- request to PHP data fetcher 
          dataType: "json", 
          success: function(data) { 
           // you get an object for iteration, see console for keys 
           console.log(data); 
    
           // table for title_popular 
           var str = '<table>'; 
           str += "<thead><th>Index</th><th>id</th><th>title</th><th>title_desc</th><th>year</th><th>description</th></thead>"; 
    
           $.each(data.title_popular, function(index, element) { 
            str += "<tr>"; 
            str += "<td>" + index + "</td>"; 
            $.each(element, function(key, element) { 
             str += "<td>" + element + "</td>"; 
            }); 
            str += "</tr>"; 
           }); 
    
           str += '</table>'; 
    
           // table for title_exact 
           str += '<table>'; 
           str += "<thead><th>Index</th><th>id</th><th>title</th><th>title_desc</th><th>year</th><th>description</th></thead>"; 
    
           $.each(data.title_exact, function(index, element) { 
            str += "<tr>"; 
            str += "<td>" + index + "</td>"; 
            $.each(element, function(key, element) { 
             str += "<td>" + element + "</td>"; 
            }); 
            str += "</tr>"; 
           }); 
    
    
           // insert the html 
           $("#SampleResults").html(str); 
          }, 
          error: function (request, status, error) { alert(status + " - " + error); } 
         }); 
        } 
        </script> 
    </head> 
    <body> 
    
    
    <!-- search textbox --> 
    <input type="text" id="title" placeholder="Enter Name for search"> 
    
    <!-- do search button --> 
    <button type="text" id="SampleSearchButton">Search</button> 
    
    <!-- display results container --> 
    <div id="SampleResults"></div> 
    </body> 
    </html> 
    

    結果

    enter image description here

  • +0

    另外一個問題我不熟悉php你解釋我如何將PHP和HTML結合在一起? – Jamiex304

    +0

    您需要一個PHP支持的網絡服務器。您只需將兩個文件放在同一個文件夾中,然後在瀏覽器中打開html文件即可。本地開發網絡服務器也可以工作!你有哪個操作系統? –

    +0

    我有Windows,xampp工作我有 – Jamiex304