Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

x-www-form-urlencoded and $resource #1937

Closed
@edgeonetechnologies

Description

@edgeonetechnologies

My backend is PHP and I followed this guide
http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/

app.config(function($httpProvider) {

    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data)
    {
        return angular.isObject(data) && String(data) !== '[object File]' ? jQuery.param(data) : data;
    }];

}); 

This allows me to use $http.post to sent POST data to my server.
Without doing this PHP cannot even see any POST data since it has no idea how to parse a JSON object.

$http.post works fine as in the example below

$http.post('ajax/campaigns/123', {id: '123', 'title':'test title'});

What I am having issues with is with $resource.
After following that guide and trying to run the following code

.factory('Campaign', function($resource) {
    return $resource('ajax/campaigns/:id', {id:'@id'});
})
var updatedCampaign = new Campaign({
    id: 123,
    title: "test title"
});

updatedCampaign.$save(function(saveData) { });

When I run the save function, it freaks out and tries to make many requests: GET, POST, GET, DELETE, DELETE, POST

It should be making a single POST request. It seems like it is running through many of the possible functions of a $resource object.

Has anyone had success using a $resource to send POST data to a PHP backend or am I missing something here?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions