Solved

AngularJS HttpProviderTransform Request Shorthand?

Posted on 2014-04-19
4
416 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
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
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)

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now