Solved

AngularJS HttpProviderTransform Request Shorthand?

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

623 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