javascript - angularjs - 语法错误 : Unexpected token when request return from server

标签 javascript c# angularjs web-services

我使用 Angular 作为我的前端,并使用 C# asmx 服务来与数据库相对应。

我有验证用户密码的服务。 C# 服务返回密码,Angular 服务验证密码。

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);

    }
}

Angular 服务:

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# 服务运行良好。反响良好。我知道这不是最佳实践或不安全,请把它放在一边。

Angular 服务抛出

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

你能帮我解决这个问题吗? 谢谢。

最佳答案

这是因为您返回字符串并且无效 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 }
        })

关于javascript - angularjs - 语法错误 : Unexpected token when request return from server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36279130/

相关文章:

javascript - JavaScript 检查数字是否为偶数

JavaScript RegExp 匹配忽略 HTML 的文本

c# - "Fold"LINQ 扩展方法在哪里?

javascript - 如何有条件地应用 Angular 状态提供者

javascript - 使用 Parse Cloud Code 一次更新多个对象

javascript - 表单字段数量向上/向下按钮,不允许负数?

c# - 与 Windows 7 相比,为什么 System.Drawing.Image.GetPropertyItem 在 Windows XP/2003 上的行为不同

c# - Linq 'first' 问题

javascript - 检查一个对象中的项目是否存在于另一个对象中(使用下划线)

javascript - 给剑道数据源一个 Angular 范围变量