简体   繁体   English

无法将base64图像代码发送到数据库

[英]unable to send base64 image code to database

i am trying to send registration details to mysql database from ionic application. 我正在尝试从离子应用程序向mysql数据库发送注册详细信息。 i am sure about my web service is working well but when i send base64 image code it gives XMLHttpRequest cannot load http://nightfoxtechnologies.in/locationbasedreminder/index.php . 我确定我的Web服务运行良好,但是当我发送base64图像代码时,它给出的XMLHttpRequest无法加载http://nightfoxtechnologies.in/locationbasedreminder/index.php Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response. 飞行前响应中的Access-Control-Allow-Headers不允许请求标头字段Content-Type。 error. 错误。

i have checked this using postman also but same issue but without base64 data is successfully inserted to database problem occurs when i send base64 image code. 我已经使用邮递员检查了此问题,但同样的问题,但是没有将base64数据成功插入数据库时​​,当我发送base64图像代码时会发生问题。

i have checked my base64 code it is correct. 我已经检查了我的base64代码,它是正确的。

tab-signup.html tab-signup.html

<div class="tabs-striped  tabs-background-positive tabs-color-light">
    <div class="tabs">
      <a class="tab-item " href="#/app/login"><i class="icon ion-unlocked"></i> Login </a>
      <a class="tab-item active" href="#/app/signup"> <i class="icon ion-person"></i> Sing Up </a>
    </div>
  </div>

<ion-view view-title="Sign Up" class="loginBg" hide-nav-bar="true" >
  <ion-content class="padding">

  <div class="row responsive-sm padding remvoeBP">
      <div class="col">
           <div class="app-logo"><img src="img/logo.png"></div>

            <div class="list list-inset removePM">
              <label class="item item-input"><input type="text" ng-model="user.username" placeholder="User Name"> </label>
              <label class="item item-input"><input type="text" ng-model="user.password" placeholder="Password"> </label>
                  <script>
                function readImage() {
                    if (this.files && this.files[0]) {
                        var FR = new FileReader();
                        FR.onload = function (e) {
                            $('#myimg').attr("src", e.target.result);
                            localStorage.base64String=e.target.result;
                            $('#base').text(e.target.result);
                        };
                        FR.readAsDataURL(this.files[0]);
                    }
                }
                $(document).ready(function(){
                    $("#myid").change(readImage);
                });
            </script>    
            <img id="myimg" src="" height="100px" width="100px" />
            <div id="base" style="display:none"></div>

              <label class="item item-input"><input type="file" ng-model="user.photo" id="myid" placeholder="Choose Image File"> </label>
              <label class="item item-input"><input type="password" ng-model="user.contactno" placeholder="Contact Number"> </label>
            </div>

            <div class="loginButton"><button class="button ion-person button-block button-balanced" ng-click="register(user);"> Register </button>  </div>
      </div>
  </div>



  </ion-content>
</ion-view>

Controller.js Controller.js

angular.module('starter.controllers', [])
.controller('doRegistrationCtrl', function($scope,$http, $stateParams , Profiles) {

        $scope.register = function(user) {

        if(typeof(user)=='undefined'){
            $scope.showAlert('Please fill username and password to proceed.');  
            return false;
        }
        var jsonString={};
        jsonString.uname=user.username;
        jsonString.pass=user.password;

        jsonString.photo=localStorage.base64String; //here my base64 image code will be there 
        jsonString.contact=user.contactno;

        $http.post("http://nightfoxtechnologies.in/locationbasedreminder/index.php", {
        action: "doRegister",
        data: JSON.stringify(jsonString)
    })
        .then(function(response) {
            //First function handles success
            console.log("suucess:::"+JSON.stringify(response.data));

            $location.path('/app/dashboard');
        }, function(response) {
            //Second function handles error
            $scope.showAlert('Invalid username or password.');  
        });


    };
    $scope.profile = Profiles.get($stateParams.profileId);
})

Even though you're using $http.post(), you are sending the entire thing inside a querystring parameter in the URL itself. 即使您使用的是$ http.post(),也要在URL本身的querystring参数中发送整个内容。 Move it out of the GET and into the POST: 将其移出GET并进入POST:

$http.post("http://nightfoxtechnologies.in/locationbasedreminder/index.php" {
    action: "doRegister",
    data: jsonString
})
.then();

或者您可以为您的应用程序使用Cordova文件传输

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM