2016-03-29 14 views
1

DBに対応するフロントエンドとC#asmxサービスにangleを使用しています。angularjs - SyntaxError:リクエストがサーバーから返されたときに予期しないトークンが返される

私は、ユーザーのパスワードを認証するサービスを提供しています。 C#サービスはパスワードを返し、角度サービスの検証が行われます。

C#のサービス:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public void Auth(string username) 
{ 
    string password = ""; 
    using (SqlConnection con = new SqlConnection(GetConnectionString())) 
    { 
     SqlCommand cmd = new SqlCommand("Select PASSWORD from users where USERNAME = '" + username + "'", con); 

     //Open Connection 
     con.Open(); 

     //To Read From SQL Server 
     SqlDataReader dr = cmd.ExecuteReader(); 

     while (dr.Read()) 
     { 
      password = dr["PASSWORD"].ToString(); 
     } 

     //Close Connection 
     dr.Close(); 
     con.Close(); 
     this.Context.Response.ContentType = "application/json; charset=utf-8"; 
     this.Context.Response.Write(password); 

    } 
} 

角度サービス:

service.Login = function (username, password, callback) { 
     var q = $q.defer; 
     $http({ 
      url: 'services/ShiftLogService.asmx/Auth', 
      dataType: "json", 
      method: "POST", 
      data: { username: username } 
     }).success(function (data) { 
      console.log(data); 
     }).error(function (data) { 
      // q.reject(); 
     }); 
     return q.promise; 

    }; 

C#のサービスは細かい動作します。応答は良いです。私はこれがベストプラクティスでも安全でもないことを承知しています。

角度サービスは

SyntaxError: Unexpected token l 
at Object.parse (native) 
at uc (http://localhost:15380/bower_components/angular/angular.min.js:17:6) 
at ac (http://localhost:15380/bower_components/angular/angular.min.js:90:253) 
at http://localhost:15380/bower_components/angular/angular.min.js:91:164 
at q (http://localhost:15380/bower_components/angular/angular.min.js:7:355) 
at ed (http://localhost:15380/bower_components/angular/angular.min.js:91:146) 
at c (http://localhost:15380/bower_components/angular/angular.min.js:92:403) 
at http://localhost:15380/bower_components/angular/angular.min.js:128:305 
at m.$eval (http://localhost:15380/bower_components/angular/angular.min.js:142:467) 
at m.$digest (http://localhost:15380/bower_components/angular/angular.min.js:140:47)(anonymous function) @ angular.js:13363(anonymous function) @ angular.js:10162(anonymous function) @ angular.js:15621m.$eval @ angular.js:17059m.$digest @ angular.js:16771m.$apply @ angular.js:17121g @ angular.js:11298x @ angular.js:11561v.onload @ angular.js:11624 

あなたはこれで私を助けてくださいことはできスロー?おかげさまで

+0

あなたはネットワーク]タブを開いて、あなたの応答をチェックすることができ、応答が有効なJSON –

+0

ないようなあなたの方法 '無効である理由、それが見えます'? 'string'を返すほうが良いのではないでしょうか?そしてJSONは戻りデータを直列化しますか? Webサービスは、応答ストリームに書き込むのではなく、 'return'ステートメントを使ってデータを返すと考えました。たぶんそれは問題ではありませんが、JSONがJSON文字列としてデータをシリアル化し、レスポンスストリームに書き込む必要があると思います。 '{password:" password "}' –

答えて

1

これは、文字列とが有効でないjsonを返すためです。

public class PassWordClass{ 
    public string Password {set; get;} 
} 

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public PassWordClass Auth(string username) 
{ 
    string password = ""; 
    using (SqlConnection con = new SqlConnection(GetConnectionString())) 
    { 
     SqlCommand cmd = new SqlCommand("Select PASSWORD from users where USERNAME = '" + username + "'", con); 

     //Open Connection 
     con.Open(); 

     //To Read From SQL Server 
     SqlDataReader dr = cmd.ExecuteReader(); 

     while (dr.Read()) 
     { 
      var pass = new PassWordClass(); 
      pass.Password = dr["PASSWORD"].ToString(); 
      return pass; 
     } 
    } 
} 

この変換要求されたJSONの正しい方法である:

$http({ 
      url: 'services/ShiftLogService.asmx/Auth', 
      responseType : "json", //<-- change like that 
      method: "POST", 
      data: { username: username } 
     }) 
+0

これはすでに試しました。予期しないトークンは '{' –

+0

と同じですが... –

+0

@SaarKuriel編集の回答 –

関連する問題