Ionic and AngularJS HTTPS Post Requset
我试图通过Ionic应用程序中的post方法请求HTTPS服务器。 但每一次,我都失败了。 我试过两种方式。 一种方法是使用
- 使用$ http:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $http({ url:"https://beta.test.com/auth/authenticate/", method:"POST", data: { "userName":"[email protected]", "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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $.ajax({ url:"https://beta.test.com/auth/authenticate/", type:"POST", data: { "userName":"[email protected]", "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'); }); |
怎么能解决这个问题? 代码有什么问题?
这是让你入门的东西,如果你提供一些更多的代码,我会更新。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | (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":"[email protected]", "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; }); } } })(); |