English 中文(简体)
我的返回数组不是作为对象返回
原标题:My returned array is not returning as an object

我返回的阵列正以这种无效的奇怪格式返回。 至少在我的 AJAX 经验中并非如此 。

function get_within($latitude, $longitude) { 
global $pdo;

  // $lat = $latitude;
  // $long = $longitude;
  // $long =  -70.98245279999999 ;
  // $lat =  41.98156549999999 ;

    $stmt = $pdo->prepare("SELECT menu_id, latitude, longitude ( acos(
          cos( radians(".$latitude."   ) ) *
            cos( radians( latitude) ) * 
            cos( radians(".$longitude.") - radians(longitude)) +
            sin( radians(".$latitude.") ) * sin( radians(latitude) ) ))*.621371192
as dis from pages where 1 ORDER BY `dis` ASC LIMIT 10");

$stmt->execute( array() );

return $stmt->fetchAll( PDO::FETCH_OBJ );

我使用上述函数提出了 AJAX 请求。我需要它返回一个对象,但它只是返回一个像这样的阵列:

[{"id":"26","subject_id":"5","menu_name":"Bobby Byrne s Food & Pubu200e","position":"1","visible":"1","content":"Bobby Byrne s Restaurant & Pubu200e 65 Massachusetts 6A, Sandwich, MA (508) 888-6088","longitude":"-70.508598","latitude":" 41.765754","dis":"0.00245126419033985"}]

什么是错的,我怎样才能回到我的查询中正确的对象,以便正确使用?

最佳回答

您的回复是 JSON 编码对象。 您需要将 JSON 解码为对象 。

Please see: http://php.net/manual/en/ref.json.php

问题回答

@DanArmstrong指出, JSON 阵列的返回没有任何奇怪或无效之处。 Per 文件 /PDO Statement::fetchall 理应返回一个阵列,而当您将阵列编成 JSON 时,它使用 array notation , 这是有效的 JSON 和您在这里看到的。

如果您想要 JSON 对象, 而不是数组, 您需要同时 :

  1. On the server side get an item out of the array returned by fetchAll and encode that value (e.g. json_encode( $results[0] )) instead of encoding the entire returned array, or
  2. On the client side get an item out of the decoded array e.g. JSON.parse( data )[0].




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签