无法以离子形式将表单数据发布到PHP API

编程入门 行业动态 更新时间:2024-10-11 15:15:55
本文介绍了无法以离子形式将表单数据发布到PHP API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

// 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等,但没有任何效果.我试图在本地主机上运行此Api,并且所有访问控制标头均在api中给出. Api接受两个参数(post方法)将其保存到数据库中,并使用两个键返回json数据. API无法获取这两个参数

I used Json.stringfy , angulartojson etc but nothing works . I am trying to run this Api on localhost and all access controls headers are given in the api. Api accepts two parameters(post method) save it to database and return json data with two keys. API is unable get those two parameters

推荐答案

感谢您的回答.其实问题是我没有在php api中正确设置标头变量.我用下面的代码解决了.

Thanks for the answer. Actually the problem was i am not setting the header variables in php api properly. I solved it using below code.

header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 86400'); // cache for 1 day

更多推荐

无法以离子形式将表单数据发布到PHP API

本文发布于:2023-10-12 05:05:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483792.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   离子   形式   数据   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!