Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AngularJS HttpProviderTransform Request Shorthand?

Posted on 2014-04-19
4
Medium Priority
?
424 Views
Last Modified: 2014-04-21
I found this very nice article concerning AngularJS and Web Api 2 here...

Q. Why is there so much code in the 'app.config(['$httpProvider' ?

Q. Does my code below really do the same thing?

The following code works great, just much smaller code footprint.

app.config(['$httpProvider', function ($httpProvider)
    {
        $httpProvider.defaults.transformRequest = function (obj)
        {
            var str = [];

            for (var p in obj)
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));

            return str.join("&");
        }
    }]);

Open in new window

0
Comment
Question by:WorknHardr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 1000 total points
ID: 40011735
Q1. The  $httpProvider.defaults.transformRequest in the article does more.

It processes an object where the values are also objects, or where the values are arrays, or a flat object like the expectation in the code you have above.

it does some type checking at the bottom to either parse the object if an object is provided, or just return the data provided.

Q2. Your code will work no problem as long as the data you provide is a non-empty object which contains just key value pairs - no nested objects. As long as you know that your app only needs to process this type of object, you're good to go. Nicer smaller footprint it is. You can always add more functionality as your project grows.

Hope that helps.
0
 

Author Comment

by:WorknHardr
ID: 40012424
Great explanation!
0
 

Author Closing Comment

by:WorknHardr
ID: 40013381
thx
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40013436
:)
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question