2016-02-23 103 views
0

我剛剛創建了一個離子應用程序,它只有一個註冊頁面。我正在做的是在後端使用php RESTful Web服務,MySql是數據庫。但我得到這個錯誤:「SyntaxError:意外的令牌<」。我不知道如何解決這個錯誤。請指導我解決這種類型的錯誤。SyntaxError:Unexpected token <at Object.parse(native)

我的index.html是:

<ion-content class="has-header" ng-controller="SignupCtrl"> 
    <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" ng-click="signup(userdata)">SignUp</button><br> 
    <span>{{responseMessage}}</span> 
</div> 
</ion-content> 

我的控制器代碼是:

.controller('SignupCtrl', function($scope, $http) { 
$scope.signup = function (userdata) { 
    var request = $http({ 
     method: "POST", 
     url: "http://localhost/lastLog.php", 
     crossDomain : true, 
     data: { 
      email: userdata.email, 
      password: userdata.password, 
      username: userdata.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" 
     } 
    }); 
} 
}) 

我的PHP代碼:

<?php 
    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 = $postdata->email; 
    $password = $postdata->password; 
    $username = $postdata->username; 

    $con = mysql_connect("localhost","root",'') or die ("Could not connect: " . mysql_error());; 
    mysql_select_db('db_lastLog', $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"; 
    } 
?> 
+0

難道你有你的錯誤行號? –

+0

它說:「在fromJson(http:// localhost:8100/lib/ionic/js/ionic.bundle.js:14543:14)」 –

+0

php腳本在服務器端執行。 –

回答

1

SyntaxError: Unexpected token <通常意味着有一個在你的JSON錯誤響應,因爲錯誤的位置在fromJson (angular.fromJson)這意味着它沒有成功諷刺JSON響應。如果您在控制檯打開network選項卡,你檢查的要求,響應很可能開始於一個<,一旦你解決這個錯誤/ JSON的應該是工作的罰款

+0

我想問一個愚蠢的問題,我必須爲此創建一個json文件嗎? –

+0

不,不是,(不是說familla用php),但你必須使用'json_encode()'返回你的數據我認爲 –

+0

如何解決錯誤/ json? –

相關問題