-1

So, I spent some time and built a quick API for a project that I'm doing for myself.

I used the Postman add-on for Chrome to mimic PUT and DELETE quests to make sure everything worked correctly. Really happy I did that, as I learned a lot about PHP's shortcomings with PUT and DELETE requests.

Using the API I've had working with Postman, I started moving everything over to AngularJs controllers and such.

I'm trying to get a user to claim a row in a database as the login information for the users is different than this particular information. I couldn't figure out why the put requests to claim the row in my database wasn't working. Lo and behold, the data being parsed from my parsestr(file_get_contents('php://input')) had 1 array key, which was a JSON string.

I've looked, and I can't seem to find a solid answer either through Stackoverflow or Google (maybe I missed it somewhere in the config options), So my question is this: is there any way I can get the $http.put call send the data to the server correctly?

Jhecht
  • 4,407
  • 1
  • 26
  • 44
  • This should help http://stackoverflow.com/questions/11442632/how-can-i-make-angular-js-post-data-as-form-data-instead-of-a-request-payload. The standard practice otherwise is to pass data as json – Chandermani Aug 25 '14 at 07:48
  • I'm assuming that the `$.param` function is a part of jQuery, because I tried it in my code and it's not working. – Jhecht Aug 25 '14 at 08:05
  • Maybe yes, search how to encode the json data form encoding. – Chandermani Aug 25 '14 at 08:11
  • Man that's annoying on the part of the angular guys. Thanks for the quick reply. – Jhecht Aug 25 '14 at 08:12

1 Answers1

0

Thanks to user Chandermani for pointing me to the link at this URL which answered the base of my question.

From the above link, I found myself on This Blog post submitted by another user. In the end, what I ended up doing was the following:

taking param() function from the above link, as well as implementing these lines of code:

var app = angular.module('ucpData', [] , function($httpProvider){
        $httpProvider.defaults.transformRequest = [function(data) {
            return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
          }];
    });

Is how I worked around the problem. For some developers, you may actually want to keep the default transformRequest settings, but for the project I am doing I know that I will end up forgetting to call param() at some point, and my server doesn't naturally accept json data anyway. I would caution future developers to consider what they are attempting to do before they alter the transformRequest array directly.

Community
  • 1
  • 1
Jhecht
  • 4,407
  • 1
  • 26
  • 44