Reputation: 5
http://samples.openweathermap.org/data/2.5/history/city?q=London,UK&appid=XXXXXXXXX
this is The link which displays the Historical weather of London
I want ajax call in Laravel 5.3 to display data retrieved by above link.
is anyone knowing how to make an Asynchronous call in laravel + Ajax
Upvotes: 1
Views: 2720
Reputation: 351
For Laravel Async you can use Laravel Queues
Implementation will be something like this
Make a new job it will contain
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
}
Please be aware that this method will run in the background.
For JS Ajax request (suppose you import 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
}
});
Upvotes: 1
Reputation: 13259
$.ajax({
type: "GET",
url:'http://samples.openweathermap.org/data/2.5/history/city?q=London,UK&appid=XXXXXXXXX',
success: function( response) {
console.log(response)
}
});
You can then change the URL to whatever you want. Be it laravel or directly to the wather API.
If you make the call to Laravel, then in Laravel, use Guzzle to make the call to the weather API and do whatever you want to with the result in the controller.
Upvotes: 0