Jake The Coder
Jake The Coder

Reputation: 23

Unable to get $http response

When I make a request using below code my C# method does not get any data:

$http({
    method : 'POST',
    url : ...,
    data : {
        test : 'hello'
    })
    .then(function (result) {
        console.log('success')
    }, function () {
        console.log('error')
    })

In debug mode I'm able to hit the method but no data is passed with the request.

Upvotes: 2

Views: 60

Answers (1)

Chris Hermut
Chris Hermut

Reputation: 1758

It's a common mistake. Your call should look like:

$http({ method: 'POST',
        url: ...,
        data: $httpParamSerializerJQLike({ test: 'hello' }),
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
)
.then(function () { console.log('success') }, 
      function () { console.log('error') })

Also don't forget to inject $httpParamSerializerJQLike

For more in depth explanation -> AngularJs $http.post() does not send data

Upvotes: 2

Related Questions