1

I'm using angular for my frontend and C# asmx service to correspond with the DB.

I have service for authenticating a user's password. The C# service returns the password and the angular service validates is.

C# service:

[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:

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;

    };

The C# service acts fine. The response is good. I know this is not best practice or secure, leave it aside please.

The angular service throws

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

Can you help me with this please? Thanks.

2
  • Could you open the network tab and check your response, it looks like the response isn't valid JSON Commented Mar 29, 2016 at 8:33
  • Why is your method void? Would it not be better to return a string, and JSON serialise the return data? I thought that a web service would return the data using return statement, rather than writing to the response stream. Maybe that doesn't matter, but I think you still should JSON serialise the data, as a JSON string, and write that to the response stream e.g. { password: "password" } Commented Mar 29, 2016 at 8:34

1 Answer 1

1

This is because you are returning string and not valid 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;
        }
    }
}

this is correct way of transform requested json:

 $http({
            url: 'services/ShiftLogService.asmx/Auth',
            responseType : "json", //<-- change like that
            method: "POST",
            data: { username: username }
        })
Sign up to request clarification or add additional context in comments.

1 Comment

Better now, at least the syntax error is gone. But, now the data returned is always null. If I test the C# service alone, it does return the object as expected, I can even see when debugging in VS. Why is the data null?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.