Cordova使用AngularJS将数据发布到Mysql数据库不起作用


Cordova Post Data to Mysql Database using AngularJS Doesn't Work

我已经在网上试过了,它运行正常,但是当我在安卓中实现时,post数据没有正常运行。

这是控制器的代码

.controller('SignupCtrl', function($scope, $http) {
$scope.signup = function () {
        var request = $http({
            method: "post",
            url: "http://myurl.com/signup.php",
            crossDomain : true,
            data: {
                email: $scope.email,
                password: $scope.password,
                username: $scope.username
            },
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        });
        /* Successful HTTP post request or not */
        request.success(function(data) {
            if(data == "1"){
             $scope.responseMessage = "Successfully Created Account";
            }
            if(data == "2"){
             $scope.responseMessage = "Create Account failed";
            }
            else if(data == "0") {
             $scope.responseMessage = "Email Already Exist"
            }  
        });
}
})

这为我的 HTML 代码

  <ion-view title="Signup">
    <ion-nav-buttons side="left">
      <button menu-toggle="left" class="button button-icon icon ion-navicon"></button>
    </ion-nav-buttons>
    <ion-content class="has-header">
      <div class="list list-inset">
       <label class="item item-input">
         <input class="form-control" type="text" ng-model="username" placeholder="Enter Username">
       </label>
       <label class="item item-input">
         <input type="text" ng-model="email" placeholder="Enter Your Email">
       </label>
       <label class="item item-input">
         <input class="form-control" type="password" ng-model="password" placeholder="Enter Your Password">
       </label>
       <button class="button button-block button-positive button-dark" ng-click="signUp()">SignUp</button><br>
       <span>{{responseMessage}}</span>
     </div>
    </ion-content>
  </ion-view>

这是为了我的PHP代码

<?php  
// check username or password from database
header("Content-Type: application/json; charset=UTF-8");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $request->email;
$password = $request->password;
$username = $request->username;
$con = mysql_connect('localhost', 'username', 'password') or die ("Could not connect: " . mysql_error());;
mysql_select_db('database_name', $con);
$qry_em = 'select count(*) as cnt from users where email ="' . $email . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);
if($res['cnt']==0){
$qry = 'INSERT INTO users (name,pass,email) values ("' . $username . '","' . $password . '","' . $email . '")';
$qry_res = mysql_query($qry);
    if ($qry_res) {
        echo "1";
    } else {
        echo "2";;
    }
}
else
{
    echo "0";
}
?>

在xD之前感谢

这是我的日志猫输出

11-26 16:52:46.868: I/v8(17709): [17709]    40790 ms:  Mark-sweep 5.3 (16.9) -> 4.8 (16.9) MB, 0 ms (+ 79 ms in 1 steps since start of marking, biggest step 79.320068 ms) [idle notification: finalize incremental] [GC in old space requested].
11-26 16:52:47.802: W/ResourceType(17709): No package identifier when getting name for resource number 0x00000064
11-26 16:52:47.804: I/View(17709): Touch down dispatch to org.apache.cordova.CordovaWebView{41edb478 VFEDH.C. .F...... 0,0-480,762 #64}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=160.66528, y[0]=351.51312, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16710764, downTime=16710764, deviceId=3, source=0x1002 }
11-26 16:52:47.831: D/AwContents(17709): setContentsSize 320, 508
11-26 16:52:47.832: D/AwContents(17709): setPageScaleFactor 1.0
11-26 16:52:47.900: W/ResourceType(17709): No package identifier when getting name for resource number 0x00000064
11-26 16:52:47.900: I/View(17709): Touch up dispatch to org.apache.cordova.CordovaWebView{41edb478 VFEDH.C. .F...... 0,0-480,762 #64}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=160.66528, y[0]=351.51312, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16710863, downTime=16710764, deviceId=3, source=0x1002 }
11-26 16:52:47.912: D/WebViewCallback(17709): shouldInterceptRequest=http://artivist.gallery/cepjuna/login.php
11-26 16:52:47.913: D/WebViewCallback(17709): onLoadResource=http://artivist.gallery/cepjuna/login.php
11-26 16:52:47.927: D/AwContents(17709): setContentsSize 320, 508
11-26 16:52:47.927: D/AwContents(17709): setPageScaleFactor 1.0
11-26 16:52:48.123: D/AwContents(17709): setContentsSize 320, 508
11-26 16:52:48.123: D/AwContents(17709): setPageScaleFactor 1.0
11-26 16:52:55.522: I/v8(17709): [17709]    49445 ms:  Mark-sweep 5.0 (16.9) -> 3.9 (15.9) MB, 1 / 0 ms (+ 95 ms in 1 steps since start of marking, biggest step 95.473877 ms) [idle notification: finalize incremental] [GC in old space requested].
11-26 16:52:58.865: I/v8(17709): [17709]    52788 ms:  Mark-sweep 4.3 (15.9) -> 3.2 (14.9) MB, 0 ms (+ 49 ms in 1 steps since start of marking, biggest step 49.250977 ms) [idle notification: finalize incremental] [GC in old space requested].
11-26 16:53:06.004: I/v8(17709): [17709]    59927 ms:  Mark-sweep 3.2 (14.9) -> 3.2 (14.9) MB, 0 ms (+ 47 ms in 1 steps since start of marking, biggest step 47.226807 ms) [idle notification: finalize incremental] [GC in old space requested].
11-26 16:53:09.873: I/v8(17709): [17709]    63796 ms:  Mark-sweep 3.2 (14.9) -> 2.9 (14.9) MB, 0 ms [idle notification: finalize idle round] [GC in old space requested].
11-26 16:53:13.910: I/v8(17709): [17709]    67833 ms:  Mark-sweep 2.9 (14.9) -> 2.9 (14.9) MB, 0 ms [idle notification: finalize idle round] [GC in old space requested].
11-26 16:53:24.033: D/dalvikvm(17709): threadid=22: exiting
11-26 16:53:24.033: D/dalvikvm(17709): threadid=22: detach (group=0x41917ce0)
11-26 16:53:24.034: D/dalvikvm(17709): threadid=22: removing from list
11-26 16:53:24.035: D/dalvikvm(17709): threadid=22: bye!
11-26 16:53:35.042: D/dalvikvm(17709): threadid=14: exiting
11-26 16:53:35.042: D/dalvikvm(17709): threadid=14: detach (group=0x41917ce0)
11-26 16:53:35.043: D/dalvikvm(17709): threadid=14: removing from list
11-26 16:53:35.043: D/dalvikvm(17709): threadid=14: bye!

试试这个

<ion-nav-buttons side="left">
      <button menu-toggle="left" class="button button-icon icon ion-navicon"></button>
    </ion-nav-buttons>
    <ion-content class="has-header">
      <div class="list list-inset">
       <label class="item item-input">
         <input class="form-control" type="text" ng-model="userdata.username" placeholder="Enter Username">
       </label>
       <label class="item item-input">
         <input type="text" ng-model="userdata.email" placeholder="Enter Your Email">
       </label>
       <label class="item item-input">
         <input class="form-control" type="password" ng-model="userdata.password" placeholder="Enter Your Password">
       </label>
       <button class="button button-block button-positive button-dark" ng-click="signUp(userdata)">SignUp</button><br>
       <span>{{responseMessage}}</span>
     </div>
    </ion-content>
  </ion-view>

在控制器中

.controller('SignupCtrl', function($scope, $http) {
$scope.login = function (userdata) {
    $http.post('http://myurl.com/signup.php',userdata).success(function(data){
           alert(data);
     });
   }
})

.php

$postdata = file_get_contents("php://input");
$email = $postdata->email;
$password = $postdata->password;
$username = $postdata->username;
//run your query

现在它有效,我改变了

  data: {
            email: $scope.email,
            password: $scope.password,
            username: $scope.username
        }

 data: {
            'email': $scope.email,
            'password': $scope.password,
            'username': $scope.username
        }

如果我不在数据上使用",则发布数据不起作用。

感谢您的建议@shammon :)

对不起,我的英语不好。 :)