π¬ AngularJS HTTP Methods: GET, POST, PUT, DELETE, JSONP β Complete API Integration Guide (2025)
π§² Introduction β Why Learn AngularJS HTTP Methods?
Most AngularJS applications rely on external data sources or APIs. Whether you’re fetching a list of products, submitting a form, updating a user profile, or deleting a record, the $http
service gives you full control over these HTTP methods: GET, POST, PUT, DELETE, and JSONP.
Understanding these methods helps you:
- Build dynamic and data-driven SPAs
- Perform CRUD operations on backend data
- Handle API responses and errors properly
π― In this guide, youβll learn:
- The syntax and use cases of each HTTP method in AngularJS
- Real-world examples using
$http
- How to handle JSONP for cross-domain requests
- Tips for headers, configuration, and best practices
βοΈ Overview of HTTP Methods
Method | Purpose | Use Case Example |
---|---|---|
GET | Retrieve data | Fetch user list |
POST | Send new data to the server | Create a new user |
PUT | Update existing data | Edit user profile |
DELETE | Remove data from the server | Delete a blog post |
JSONP | Cross-domain GET using script tags | Fetch public data from an API |
β 1. GET β Retrieve Data
Use GET
to read data from an API or server.
$http.get("https://api.example.com/users")
.then(function(response) {
$scope.users = response.data;
})
.catch(function(error) {
console.error("GET Error:", error);
});
β Use When: Displaying lists, tables, reports, charts, etc.
β 2. POST β Send Data
Use POST
to submit data, such as forms or new records.
$http.post("https://api.example.com/users", {
name: "Alice",
email: "alice@example.com"
})
.then(function(response) {
$scope.message = "User created successfully!";
})
.catch(function(error) {
$scope.error = "Failed to create user.";
});
β Use When: Creating new entries, form submissions.
β 3. PUT β Update Data
Use PUT
to replace or update existing records.
$http.put("https://api.example.com/users/101", {
name: "Alice Smith",
email: "alice@newdomain.com"
})
.then(function(response) {
$scope.message = "User updated.";
})
.catch(function(error) {
console.error("Update failed:", error);
});
β Use When: Updating profile info, settings, or any existing record.
β 4. DELETE β Remove Data
Use DELETE
to remove a resource identified by a unique ID or endpoint.
$http.delete("https://api.example.com/users/101")
.then(function(response) {
$scope.message = "User deleted.";
})
.catch(function(error) {
console.error("Delete error:", error);
});
β Use When: Removing items, cancelling accounts, deleting posts.
β 5. JSONP β Cross-Domain Data Fetching
JSONP (JSON with Padding) is used to overcome cross-origin restrictions when a server does not support CORS.
$http.jsonp("https://api.publicapis.org/entries?callback=JSON_CALLBACK")
.then(function(response) {
$scope.apis = response.data.entries;
});
β
JSONP requests must include ?callback=JSON_CALLBACK
.
π§ͺ Using $http
with Config Objects
For better control, use $http()
with custom config:
$http({
method: 'POST',
url: 'https://api.example.com/products',
data: { name: "Phone", price: 999 },
headers: { 'Content-Type': 'application/json' }
})
.then(function(res) {
console.log("Product added:", res.data);
});
π Adding Custom Headers
Useful for authentication or content negotiation:
$http.get("/api/secure-data", {
headers: {
'Authorization': 'Bearer ' + token
}
});
π§ Real-World Examples
Scenario | HTTP Method | Description |
---|---|---|
Show all users | GET | Load user list in dashboard |
Register new account | POST | Submit sign-up form |
Update user profile | PUT | Edit existing user info |
Delete contact from address book | DELETE | Remove data from server |
Fetch COVID stats from public API | JSONP | Access cross-origin JSON data |
π οΈ Best Practices
βοΈ Always use .catch()
for error handling
βοΈ Prefer POST
for form submissions, not GET
βοΈ Use JSONP only when CORS is unavailable
βοΈ Structure API logic in services, not controllers
βοΈ Secure requests with authentication headers if required
π Summary β Recap & Next Steps
AngularJS supports all common HTTP methods out of the box through its $http
service, making it easy to build rich, data-driven applications that interact with remote servers.
π Key Takeaways:
- Use
GET
to fetch data,POST
to create,PUT
to update, andDELETE
to remove - Use
JSONP
for cross-origin requests if CORS is unsupported - Always manage promises using
.then()
and.catch()
- Prefer services for reusable HTTP calls
βοΈ Real-world Relevance:
These methods are used in dashboards, admin panels, CRMs, eCommerce apps, real-time reporting tools, and data entry systems.
β FAQ β AngularJS HTTP Methods
β Whatβs the difference between POST and PUT in AngularJS?
β
POST
is used to create a new resource. PUT
is used to update an existing one.
β Can I send custom headers with AngularJS HTTP requests?
β
Yes. Use the headers
property inside your $http()
config.
β When should I use JSONP in AngularJS?
β
Use JSONP only when calling external APIs that do not support CORS. Ensure the server supports JSONP format.
β Are these HTTP methods asynchronous?
β
Yes. All $http
calls are asynchronous and return a promise.
Share Now :