我正在尝试通过Ionic app中的post方法向HTTPS服务器请求。但每一次,我都失败了。我试过两种方式。一种方法是使用$http
,另一种方法是使用$.ajax
。但仍未找到正确的方法。代码如下。
- 使用$ http:
$http({
url: "https://beta.test.com/auth/authenticate/",
method: "POST",
data: {
"userName": "vv9@test.com",
"password": "vv9",
"ipAddress": "2d:5d:3s:1s:3w",
"remeberMe": true
},
headers: {
'Content-Type': 'application/json'
}
}).success(function(res) {
$scope.persons = res; // assign $scope.persons here as promise is resolved here
}).error(function(res) {
$scope.status = res;
});
- 使用$ .ajax:
$.ajax({
url: "https://beta.test.com/auth/authenticate/",
type: "POST",
data: {
"userName": "vv9@test.com",
"password": "vv9",
"ipAddress": "2d:5d:3s:1s:3w",
"remeberMe": true
},
headers: {
'Content-Type': 'application/json'
}
}).done(function() {
console.log('Stripe loaded');
}).fail(function() {
console.log('Stripe not loaded');
}).always(function() {
console.log('Tried to load Stripe');
});
怎样才能解决这个问题?代码有什么问题?
答案 0 :(得分:1)
这是让你入门的东西,如果你提供一些plunker或更多的代码,我会更新。
(function() {
'use strict';
angular
.module('example.app', [])
.controller('ExampleController', ExampleController)
.service('exampleServce', exampleServce);
function ExampleController(exampleService) {
var vm = this;
vm.update = function(person, index) {
exampleService.updatePeople(person).then(function(response) {
vm.persons = response;
}, function(reason) {
console.log(reason);
});
};
}
// good practice to use uppercase variable for URL, to denote constant.
//this part should be done in a service
function exampleService($http) {
var URL = 'https://beta.test.com/auth/authenticate/',
data = {
"userName": "vv9@test.com",
"password": "vv9",
"ipAddress": "2d:5d:3s:1s:3w",
"remeberMe": true
},
service = {
updatePeople: updatePeople
};
return service;
function updatePeople(person) {
//person would be update of person.
return $http
.post(URL, data)
.then(function(response) {
return response.data;
}, function(response) {
return response;
});
}
}
})();