AngularJS Ajax example using $http

In this tutorial, we are going to call ajax in angular js using $http service. AngularJS provides multiple services out of the box. $http service can be used to perform http request. It allows you create any http request by just injecting it via controller constructor.

In this example, we are going to read countries.json using ajax request from server and will show data of it in a table.

Ajax request in app.js

As you can see here , we are injecting $http in controller function here and assigning response to countries list.

 Lets see complete example:

 Project structure:

Step 1: Create dynamic web project named “AngularJSAjaxExample” in eclipse.
Step 2: We are going to read counties.json from server. Create countries.json in WebContent folder with below json text.

Step 3: Create angularJSAjaxExample.html in WebContent folder as below:

Here we are using ng-repeat tag to iterate counties list and showing it in table.

Step 4 : Create app.js in WebContent folder as below:


Step 5: We are going to apply style on this table, so create table.css as below in WebContent folder.

Step 6: Run the html file on the server. Right click on “angularJSAjaxExample.html” and go to run -> run on server.

You will see below output:

URL: http://localhost:8080/AngularJSAjaxExample/angularJSAjaxExample.html

Source code:

click to begin
20KB .zip

Was this post helpful?

Related Posts

  • Extract numbers from String in PHP
    04 December

    Extract Numbers From String in PHP

    Table of ContentsUsing filter_var() methodUse preg_match_all() methodUse preg_replace() method Using filter_var() method We can use the filter_var() method which filters a variable based on supplied filter flag or ID. To extract numbers from the string variable, we can pass the FILTER_SANITIZE_NUMBER_INT id. Let’s see an example using this method. [crayon-638c6dd6cda43339764804/] [crayon-638c6dd6cda49584294747/] This approach isn’t efficient […]

  • 04 December

    Convert Pandas Dataframe Column to List

    Table of ContentsUse Series.values.tolist() MethodUse list() MethodConclusion Use Series.values.tolist() Method To convert pandas dataframe column to list: Use pd.DataFrame() to read position_salaries as a pandas data frame. Use df["Position"] to get the column position from df Use position.values to get values of the position Use position_values.tolist() to get list of position_values as position_list [crayon-638c6dd6cdbdc719598478/] The […]

  • 04 December

    Create Array of Objects in PHP

    Table of ContentsUsing [] syntaxUsing array() methodUsing stdClass Object An array can contain a sequence of elements of different data types. From strings to integers to floats to objects. In this post, we will discuss the different ways to how to create an array of objects in PHP. Using [] syntax We can use the […]

  • 04 December

    Get Parameters From URL String in PHP

    Use parse_url() with parse_str() functions Use parse_url() with parse_str() to get parameters from URL String in PHP. The parse_url() function is used to return the components of a URL by parsing it and parse_str() is used to create an associate array of query parameters and their values. [crayon-638c6dd6cddec742510586/] [crayon-638c6dd6cddef840525086/] Once you have the array of […]

  • 02 December

    Pass Variable From PHP to JavaScript

    Table of ContentsUse echo methodUse json_encode methodUse htmlspecialchars method Use echo method In order to pass variable from PHP to JavaScript, you will need to use the echo statement. This will print out the variable for you. For example, if you have a variable named $name, you would use the following code: [crayon-638c6dd6cdf08958736156/] This will […]

  • 02 December

    Remove Quotes from String in PHP

    Table of ContentsRemove Quotes from String in PhpUsing str_replace() MethodUsing preg_replace() MethodRemove Quotes from Beginning and End of String in PhpUsing trim() MethodUsing preg_Replace() Method Remove Quotes from String in Php Using str_replace() Method Use the str_replace() function to remove quotes from String in PHP. [crayon-638c6dd6ce017995839267/] [crayon-638c6dd6ce01a021180920/] We used str_replace() to remove double quotes and […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.