2014-06-30 104 views
0

嗨我试图获取我使用PHP获取的数据。试图从php页面获取数据使用jQuery获取json数据

页名为Twitter的fetch.php

现在我所知道的是,该页面现在充满了()的数据转换为关联数组,因为我json_decode JSON数据。 (如果我错了,请纠正我)。

现在我想要做的就是使用ajax调用该页面。

该项目的名称是手机。我的js文件位于js文件夹中。和twitter-fetch.php在php文件夹中。我只是想看到的结果在控制台现在,帮...皱眉

我还是新来的AJAX和JSON,所以我真的不知道在我在这里做什么

$(document).ready(function(){ 
    $.ajax({ 
     url: '/phone/php/twitter-fetch.php', 
     type:'POST', 
     contentType: 'application/json', 
     dataType: 'json', 
     success: function(result){ 
      console.log(result); 
      alert('success!'); 
     } 
    }); 
}); 

这个php的线,我用json_decode并将其存储在$字符串

$url = "https://api.twitter.com/1.1/search/tweets.json"; 
$requestMethod = "GET"; 

$getfield = '?q=%23forsale&result_type=recent&count=100'; 
//the TwitterAPIExchange is a PHP Wrapper that I used 
$twitter = new TwitterAPIExchange($settings); 

$string = json_decode($twitter->setGetfield($getfield) 
->buildOauth($url, $requestMethod) 
->performRequest(),$assoc = TRUE); 

UPDATE:----- $ .getJson

$.getJSON('./php/twitter-fetch.php',function(result){ 
      var output = $.map(result, function(item, index){ 
       var listitem = $('<li></li>'); 
       $('<h2>'+item.user.name+'</h2>').appendTo(listitem); 
       return listitem; 
       console.log(listitem); 
      }); 
      $('#js-result-list').html(output); 
     }); 
+0

然后是什么问题? –

+0

您是否收到成功消息? – Ninad

+0

我没有得到任何东西... – Don

回答

0

现在,您正在向链接[website directory]/phone/php/twitter-fetch.php发送请求,该请求可能不是真实文件。相反,你需要使用../回去的phone/目录,然后把php/twitter-fetch.php

$(document).ready(function(){ 
    $.ajax({ 
     url: '../php/twitter-fetch.php', 
     type:'POST', 
     contentType: 'application/json', 
     dataType: 'json', 
     success: function(result){ 
      console.log(result); 
      alert('success!'); 
     } 
    }); 
}); 

此外,请记住,当你把/在一个相对链接的开始,即相对链接被添加到网站目录。如果您希望将其添加到当前文件所在的最低目录中,请不要在开始处放置/

+0

嗨@Noble谢谢你的回复,虽然给了我一个错误。相反,我用这个./,我的PHP代码呢?我知道json_decode将json数据转换为关联数组。现在我试着回显$ string。我仍然没有得到控制台中的任何东西。 – Don

+0

我不是一个PHP人(Python FTW),但我确实知道一些PHP,并且您的PHP看起来不错。你得到一个PHP错误或JavaScript错误?如果前者,别人必须帮助你。 –