【发布时间】:2015-11-01 03:59:24
【问题描述】:
我试图从我的 php 页面获取参数到 android 应用程序,
这里是 js.php
上的代码<?php header('content-type: application/json; charset=utf-8');
$rest=$_GET['rest'];
if ($rest=="query"){
$sql = "SELECT * FROM post";
$result = $conn->query($sql);
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray['posts'] = $post;
$post[] = str_replace('"', "'", $row);
}
$json = json_encode($emparray);
echo isset($_GET['callback'])
? "{$_GET['callback']}($json)"
: $json;
}
?>
我在我的服务器上使用 .htaccess
RewriteEngine On
Options +FollowSymLinks -MultiViews
RewriteRule ^rest/([A-Za-z]+)/?$ js.php?rest=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
最后,我在 ionic 上的 js www\js\app.js
var App = angular.module('App', ['ionic']);
App.service("Robo", ["$http", "$log", Robo])
App.controller("AppCtrl", ["$scope", "Robo", "$log", AppCtrl]);
function AppCtrl($scope, Robo, $log){
$scope.posts = [];
$scope.refresh = function(){
Robo.getBlogs($scope);
}
}
function Robo($http, $log) {
this.getBlogs = function($scope){
$http.jsonp("http://www.domain.com/rest/query?callback=JSON_CALLBACK")
.success(function(result){
$scope.posts = result.posts;
$scope.$broadcast("scroll.refreshComplete");
});
}
}
在 index.html 上
<html lang="en" data-ng-app="App">
<ion-content>
<ion-refresher on-refresh="refresh()"></ion-refresher>
<div data-ng-show="!posts.length">
<br><br>
<center>
<h1>Pull to Refresh</h1>
</center>
</div>
.
.
.
我被困在这里,数组 $scope.posts 上没有数据,所以当我拉动刷新时,加载永远不会结束。请帮助并建议我解决此问题
【问题讨论】:
-
查看开发者工具的网络窗格中的内容 - 请求状态和响应正文对于检查问题的来源很重要。
-
content-type: application/json— JSONP 不是 JSON,正确的内容类型是application/javascript -
我已经更改为 app;ication/javascript 但帖子仍然无法在我的 Android 应用上显示
标签: javascript php mysql angularjs .htaccess