0

我在 json 对象中从 Yahoo Finance 提取股票符号,并尝试在用户开始在搜索框中输入公司名称或符号时将它们显示为下拉菜单。Typeahead 不能用作搜索框中的下拉菜单。我认为我做的一切都是正确的。这是我到目前为止的代码。任何帮助表示赞赏。

引用.js

$(document).ready(function() {

  // create autocomplete
  $('#form-quote input[name=symbol]').typeahead({

      // load autocomplete data from suggest.php
      source: function(query, callback) {
          $.ajax({
              url: '../suggest.php',
              type: 'POST',
              dataType: 'json',
              data: {
                  symbol: query
              },
              success: function(response) {
                  callback(response.symbols);

              }
          });
      }
  });

  // load data via ajax when form is submitted
  $('#form-quote').on('click', function() {

      // determine symbol
      var symbol = $('#form-quote input[name=symbol]').val();

      // send request to quote.php
      $.ajax({
          url: 'quote.php',
          type: 'POST',
          data: {
              symbol: symbol
          },
          success: function(response) {
              $('#price').text(response);
          }
      });


      return false;
  });

});

报价单.php

<?php

//configuration
require("../includes/config.php");

//if form was submitted 

if($_SERVER["REQUEST_METHOD"] == "POST"){

    $stock = lookup(strtoupper($_POST["symbol"]));

    if(empty($_POST["symbol"])){

        //echo "You must enter a stock symbol";

    }else if($_POST["symbol"]){

    $price = number_format($stock['price'], 2);

    echo "A share of {$stock['name']} costs $$price";
    }
}

else{

    // render portfolio
render("stock_search.php", ["title" => "Get Quote"]);
}   
?>

报价搜索.php

<form id = "form-quote" action="quote.php" method="post">
<fieldset>     
    <div class="control-group">
        <input name="symbol" autofocus autocomplete="off"  placeholder="Symbol"  type="text"/>
    </div>

    <div class="control-group">
        <button type="submit" class="btn">Get Quote</button>
    </div>

</fieldset>
<div id="price"></div>
<div id="suggestions"></div> 
</form>
<script type="text/javascript" src="js/quote.js" ></script>

建议.php

 <?php

// configuration
require("../includes/functions.php");

// if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    // load suggestion data
$data = @file_get_contents("http://d.yimg.com/aq/autoc?query=  {$_POST['symbol']}&region=US&lang=en-US&callback=YAHOO.util.ScriptNodeDataSource.callbacks");

    // parse yahoo data into a list of symbols
$result = [];
    $json = json_decode(substr($data, strlen('YAHOO.util.ScriptNodeDataSource.callbacks('), -1));
    foreach ($json->ResultSet->Result as $stock)
        $result[] = $stock;

    echo json_encode(['symbols' => $result]);
}

?>
4

4 回答 4

1

Typeahead 仅将字符串数组作为源

  // i.e.
  ["INTC", "GOOG", "FB", /* etc */]

您的脚本所做的是创建 Yahoo 返回的整个对象的数组

  // i.e.
  [
    {"symbol":"INTC","name": "Intel Corporation","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},
    {"symbol":"INTC.MX","name": "Intel Corporation","exch": "MEX","type": "S","exchDisp":"Mexico","typeDisp":"Equity"},
    /* etc */
  ]

您需要做的是更改您的Suggest.php,以便该行:

foreach ($json->ResultSet->Result as $stock)
        $result[] = $stock;

变成例如:

foreach ($json->ResultSet->Result as $stock)
        $result[] = '('.$stock->symbol.') '.$stock->name;
于 2013-04-05T17:53:37.767 回答
0

尝试像这样在 Ajax 中添加 URL 的完整路径localhost/app/user/suggest.php

于 2013-04-01T13:40:16.290 回答
0

确保在使用 json_encode 响应之前将“Content-type”设置为 json。

header("Content-type: application/json");
于 2013-04-01T13:42:44.667 回答
0

愚蠢的问题,但是您期望的 json 结果,即使将输入提前排除在等式之外,这是否会正确恢复?因为传递给 file_get_contents 的值可能会有点拙劣,除非格式在粘贴时被炸毁。

于 2013-04-03T04:16:59.957 回答