执行php脚本离子angularjs时$ http.get状态错误0

时间:2015-05-25 11:40:09

标签: ajax angularjs http ionic

在离子加载此控制器时,我收到状态错误0。 (angularjs) 在给定的ip地址上从wamp服务器调用php脚本。 它应该

.controller('FloorplanCtrl', function($scope, $http, BeaconData, DataBase, DateService) {

  $scope.path = '';
  $scope.url = "192.168.43.5/EE5/SQLReadDatabase.php";

  $http.get($scope.url)
  .success(function(response) {
    $scope.path = response[1][6];
    alert(path);
  })
  .error(function(data, status, headers, config) {
    alert("error " + status );
  });

})

它适用于此ajax请求:

$.ajax({
                    url: IPAdress + "/EE5/SQLReadDatabase.php",
                    type: "GET",
                    dataType: "json",
                    data: { type:"readDatabase"},
                    ContentType:"application/json",
                    success: function (response)
                    {
                        Array = response;
                        for(var i=0;i<Array.length;i++){
                            Array[i][2] = moment(Array[i][2]);
                            Array[i][3] = moment(Array[i][3]);

                        }
                        localStorage["messages"] = JSON.stringify(Array);
                        document.write("Done reading database");

                    }

2 个答案:

答案 0 :(得分:2)

正如你的帖子的评论所说,你似乎没有发送相同的请求{ur}中缺少type

.controller('FloorplanCtrl', function($scope, $http, BeaconData, DataBase, DateService) {

      $scope.path = '';
      $scope.url = "http://192.168.43.5/EE5/SQLReadDatabase.php";

      $http.get($scope.url+"?type=readDatabase")
      .success(function(response) {
        $scope.path = response[1][6];
        alert(path);
      })
      .error(function(data, status, headers, config) {
        alert("error " + status );
      });

    })

答案 1 :(得分:0)

AngularJS使用

传输数据
Content-Type: application/json

一些Web服务器语言 - 特别是PHP-本身不会反序列化它。这意味着您必须按照某些步骤才能使其正常工作。 将以下代码添加到您的配置

// Your app's root module...
angular.module('MyModule', [], function($httpProvider) {
// Use x-www-form-urlencoded Content-Type
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

/**
* The workhorse; converts an object to x-www-form-urlencoded serialization.
* @param {Object} obj
* @return {String}
*/ 
var param = function(obj) {
var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

for(name in obj) {
  value = obj[name];

  if(value instanceof Array) {
    for(i=0; i<value.length; ++i) {
      subValue = value[i];
      fullSubName = name + '[' + i + ']';
      innerObj = {};
      innerObj[fullSubName] = subValue;
      query += param(innerObj) + '&';
    }
  }
  else if(value instanceof Object) {
    for(subName in value) {
      subValue = value[subName];
      fullSubName = name + '[' + subName + ']';
      innerObj = {};
      innerObj[fullSubName] = subValue;
      query += param(innerObj) + '&';
    }
  }
  else if(value !== undefined && value !== null)
    query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}

return query.length ? query.substr(0, query.length - 1) : query;
};

// Override $http service's default transformRequest
$httpProvider.defaults.transformRequest = [function(data) {
return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
 }];
});

取自: http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/

我已将它应用到我的项目中并且有效,祝你好运:D