2017-02-22 61 views
1

http://samples.openweathermap.org/data/2.5/history/city?q=London,UK&appid=XXXXXXXXX想使用Ajax

这是显示伦敦

的历史天气我想在Laravel 5.3 AJAX调用显示由上面的链接检索的数据链接获取URL数据。

是任何人知道如何使异步调用在laravel +阿贾克斯

+0

您是否尝试过使用jQuery? – user2027202827

+0

总是显示你做了什么?什么是错误?所以你会得到具体的答案 –

回答

0
$.ajax({ 
    type: "GET", 
    url:'http://samples.openweathermap.org/data/2.5/history/city?q=London,UK&appid=XXXXXXXXX', 
    success: function(response) { 
     console.log(response) 
    } 
}); 

然后,您可以切换到任何你想要的网址。无论是直接还是直接访问API API。

如果您打电话给Laravel,然后在Laravel中,使用Guzzle打电话给天气API,然后根据控制器中的结果执行任何操作。

+0

有没有任何有效的上述问题的工作? ,因为我无法做到这一点,你说。 –

1

对于Laravel异步可以使用Laravel Queues

实施将是这样的

创建一个新的工作,它将包含

public function handle() 
    { 
     $appid = 'YOUR_API_KEY'; 

     $url = "http://samples.openweathermap.org/data/2.5/history/city?q=London,UK&appid=" . $appid; 
     $json = json_decode(file_get_contents($url), true); 

     dd($json); 
     //DO_SOMTHING_IN_YOUR_JSON 
    } 

请注意,这种方法会在后台运行。

对于JS Ajax请求(假设你输入的jQuery)

var appId = YOUR_APP_ID; 
var url = "http://samples.openweathermap.org/data/2.5/history/city?q=London,UK&appid=" + appId; 
$.ajax({ 
    type: "GET", 
    url: url, 
    success: function(response) { 
     //DO_SOMTHING_WITH_YOUR_JSON 
    } 
}); 
+0

你有任何工作代码吗? –

+0

对于'JS',你可以在浏览器的控制台中执行上面的代码,对于'PHP'只是试图把当前代码放在任何'php'脚本中,它现在正在工作:) –