jQuery GET JSON from URL Tutorial: Retrieve data from a PHP script

preview_player
Показать описание

That's on the assumption that the URL you are trying to send a request to actually returns a JSON response but if you have an endpoint or script that returns JSON data (or even a JSON file) then you can use jQuery to retrieve that data.

In the tutorial you will get an understanding of the way jQuery GETs JSON from a URL using the AJAX convenience function built in to jQuery. You could retrieve this data on the page load or it could be triggered on a JavaScript event for example a button click.

You'll learn how to send a GET request to a PHP script locally on your computer which in the example will just return some a basic JSON structure but in reality it could be generated from your web application such as data retrieved from a database.

jQuery has traditionally relied on callback functions built in to it's convenience functions for GET and POST AJAX requests but in the tutorial you will learn how to use jQuery's inbuilt promise chaining system. You'll also use arrow functions to provide the callbacks to this promise-like feature to make your code super tight and readable.

Using the convenience functions for AJAX the jQuery GET JSON from URL process can be simplified and you can easily connect failure handling in to the chain by ensuring the .fail method has a callback provided. Channel Handle @codebubb
Рекомендации по теме
Комментарии
Автор

dude you just saved my life rn. thank you i was so stuck! great video. :)

gamergirlmars
Автор

Hi, u want to ask, is it possible ti do something with the data aside from console.log(data)? For example, if i want to alert 'success' in the website after getting json data from url?

dynaa
Автор

when using api's to get the data e.g. wordpress api blogs, is it ok to display the url on the main page for security reasons. Or doesnt it matter? i use php an include it at moment from another file... not sure if i need to though .. thx,

rw
Автор

excellent but video sound is too low. great work anyways..Mate....

yasirtahirkheli