【问题标题】:angularjs - SyntaxError: Unexpected token when request return from serverangularjs - SyntaxError:请求从服务器返回时出现意外令牌
【发布时间】:2016-07-16 16:52:51
【问题描述】:

我正在为我的前端和 C# asmx 服务使用 angular 来与数据库相对应。

我有验证用户密码的服务。 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);

    }
}

角度服务:

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

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

【问题讨论】:

  • 您能否打开网络选项卡并检查您的响应,看起来响应不是有效的 JSON
  • 为什么你的方法是void?返回一个string,然后JSON序列化返回数据不是更好吗?我认为 Web 服务会使用 return 语句返回数据,而不是写入响应流。也许这无关紧要,但我认为你仍然应该 JSON 将数据序列化为 JSON 字符串,并将其写入响应流,例如{ password: "password" }

标签: javascript c# angularjs web-services


【解决方案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 }
        })

【讨论】:

  • 现在好多了,至少语法错误没有了。但是,现在返回的data 始终为空。如果我单独测试 C# 服务,它确实会按预期返回对象,我什至可以在 VS 中调试时看到。为什么data 为空?
猜你喜欢
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
相关资源
最近更新 更多