// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('AppCtrl', function($scope, $http) {
$scope.data = {};
$scope.submit = function(){
var link = 'api_url';
$http.post(link, angular.toJson({key: $scope.data.key,mac_address : $scope.data.mac_address})).then(function (res){
$scope.response = res.data;
});
};
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link rel="manifest" href="manifest.json">
<!-- un-comment this code to enable service worker
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js')
.then(() => console.log('service worker installed'))
.catch(err => console.log('Error', err));
}
</script>-->
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<html>
<body ng-app="starter" ng-controller="AppCtrl">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content padding="true">
<form ng-submit="submit()">
<label class="item item-input item-stacked-label">
<span class="input-label">Username</span>
<input type="text" name="key" placeholder="enter username" ng-model="data.key">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Username</span>
<input type="text" name="mac_address" placeholder="enter username" ng-model="data.mac_address">
</label>
<input class="button button-block button-positive" type="submit" name="submit" value="Submit to server">
</form>
<div class="card">
<div class="item item-text-wrap">
Response: <b ng-bind="response"></b>
</div>
</div>
</ion-content>
</ion-pane>
</body>
</html>
我使用了Json.stringfy,angulartojson等,但没有任何作用。我试图在localhost上运行此Api,并在api中给出所有访问控制头。 Api接受两个参数(post方法)将其保存到数据库并使用两个键返回json数据。 API无法获取这两个参数
答案 0 :(得分:0)
要发帖,您必须具有以下结构:
.controller('AppCtrl', function($scope, $http) {
$scope.submit = function(){
var link = 'api_url';
var data = {key: $scope.data.key, mac_address : $scope.data.mac_address};
$http.post(method : 'POST', url : link, data : JSON.stringify(data), dataType : 'json', contentType: 'application/json')).then(function (res){
$scope.response = res.data;
});
};
});
答案 1 :(得分:0)
感谢您的回答。实际上问题是我没有正确设置php api中的头部变量。我使用下面的代码解决了它。
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day