【问题标题】:Ionic and AngularJS HTTPS Post RequsetIonic 和 Angular JS HTTPS 发布请求
【发布时间】:2016-08-31 18:42:19
【问题描述】:

我正在尝试通过 Ionic 应用程序中的 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');
});

如何解决这个问题?代码有什么问题?

【问题讨论】:

  • 您遇到了什么错误,您的预期响应是什么?
  • 错误是 XMLHttpRequest 无法加载 https://beta.test.com/auth/authenticate/。预检响应包含无效的 HTTP 状态代码 404
  • 如果您可以访问服务器,那么这就是您的解决方案。stackoverflow.com/a/33820700/5686100
  • 谢谢。似乎与服务器端有关。

标签: jquery angularjs ajax ionic-framework


【解决方案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;
        });
    }
  }
})();

【讨论】:

  • 和以前一样。 XMLHttpRequest 无法加载 https://beta.test.com/auth/authenticate/。预检响应包含无效的 HTTP 状态代码 404
  • 让我更新一下,你确定你的端点是正确的。尽量不要包含 headers 类型。
  • 标头类型为'Content-Type':'application/json'。但同样的错误。
  • 如何保护使用 $http 发布的数据??
猜你喜欢
  • 2019-03-05
  • 2018-10-03
  • 2018-10-28
  • 2018-07-03
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
相关资源
最近更新 更多