Jason Jason - 7 months ago 42
Javascript Question

Adding $http request bricks Ionic app

I'm trying to add a post request to a button in my ionic app but I've found that adding the code makes the app useless. Not a single thing responds to any of my taps. Removing the code makes things normal again. This is all I have added:

$scope.powerPrompt = function() {
var pwr = alert("Power On");
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$http({
method: 'POST',
url: 'url',
data: "string"
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(response) {
// handle success things
})
.error(function(data, status, headers, config) {
// handle error things
})
};


If I delete everything inside the powerPrompt function except the var pwr line, the rest of my app works. What's causing this? Do I have a syntax issue?

Answer

You are missing a , after the data property

$scope.powerPrompt = function() {
    var pwr = alert("Power On");
    $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
    $http({
      method: 'POST',
      url: 'url',
      data: "string",
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(response) {
      // handle success things
    })
    .error(function(data, status, headers, config) {
      // handle error things
    })
};