Solved

AngularJS HttpProviderTransform Request Shorthand?

Posted on 2014-04-19
4
414 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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…

760 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

26 Experts available now in Live!

Get 1:1 Help Now