【问题标题】:Unable to retrive data from webservice ionic1 framework无法从 webservice ionic 1 框架中检索数据
【发布时间】:2017-02-12 06:36:45
【问题描述】:

我是ionic1 framework 的新手,正在sidemenu ionic app 工作。我想从Mysql Database获取播放列表。
我在我的 controller.js 中尝试过这个:

.controller('PlaylistsCtrl', function($scope,$http) {
$http.get("#/php/list.php")
.success(function (response) {

    $scope.playlists = response.records;
    });

console.log($scope.playlists);

 })

php文件代码

header("Content-Type: application/json; charset=UTF-8");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
$json[] =array('title'=>'Reggae','id'=>1);
$json[] =array('title'=>'test','id'=>2);
echo json_encode($json); 

它给出了未定义的
请帮助我哪里出错了。

【问题讨论】:

    标签: php html mysql angularjs ionic-framework


    【解决方案1】:

    您的代码异步工作。这就是您添加匿名函数来检索数据的原因。

    console.log 在请求之后但在响应之前被调用。所以$scope.playlists 是空的。

    您必须将console.log 放入成功回调中才能看到结果。

    【讨论】:

    • 非常感谢您的回答,因为我已将console.log 放在成功回调中,但没有运气它给出了未定义的。
    • 你测试console.log(response)了吗?为什么url前面有#
    • 是的,我已经测试了console.log(response) 它提供了 index.html 文件的代码。如果我删除了 URL 前面的 # ,则会出现 404 错误。
    • 您应该添加 PHP 脚本的真实 URL 作为参数 - 顺便说一句,如果您在浏览器中访问该脚本会发生什么?
    • 如果我使用像 http://example.com/webservices/list.php 这样的 URL,它会给出 XMLHttpRequest cannot load http://example.com/webservices/list.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.101:8100' is therefore not allowed access. 并直接运行 url 它会给出 json。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2012-05-26
    • 2014-04-20
    相关资源
    最近更新 更多