2017-02-22 14 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'腳本中,它現在正在工作:) –