1

I am trying to send a DELETE command using $http.delete but am getting an error:

TypeError --> Cannot create property 'method' on string 'user_id=5&response=This+is+a+test.'

when I execute the command in the controller as follows:

In the controller:

.controller(myController', function($scope) {

    var something = {
      user_id: 1,
      text: 'This is a test.'
    }

     MyFactory.deleteSomething(something);
});

In the service:

.factory('MyFactory', function($http) {

var service = {};

service.deleteSomething = function(something) {

var data = $.param(something);
var promise =  $http.delete('http://example.com/myapp.php?',
data, {
  headers: {
    'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
  }
})
  .success(function(data,status) {
    console.log("Delete success!");
    return data;
  })
  .error(function(data, status, header, config) {
    console.log("Delete FAILED!");
  });
return promise;
   }
return service;
});

What am I missing here?

1 Answer 1

3

The second parameter delete() expects is an object with http request configuration. Not some string as you are passing data:

$http.delete('http://example.com/myapp.php?', data, { [...]

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't that what var data = $.param(something); is for? Just going by this answer on SO.
As you can see in your own error message, $.param(something) is returning a string containing query parameters based on a object 'user_id=5&response=This+is+a+test.' You would probably want something that does the opposite of that.
This solved it: Change var data = $.param(something); to var data = {params: something};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.