0

I am calling my web service from angularjs

this is my code

$http.get('loginservice.asmx/validatelogin', {
                params: {
                    username: $scope.txtlogin,
                    password: $scope.txtpass
                }
            })
            .then(function (response) {
                alert('login success');
            })

this is my path when i drag and drop the web service

<a href="~/loginservice.asmx">~/loginservice.asmx</a>

but when i click on login button

I am getting error and this is how my path look like

/login/~/loginservice.asmx/validatelogin?password=123&username=minesh

the error is (the resource file cannot be found)

my web service

using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Data.SqlClient;
using TaskManager.App_Code;

namespace MVC5TMS
{
    /// <summary>
    /// Summary description for loginservice
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class loginservice : System.Web.Services.WebService
    {


        DbFunction dbf = new DbFunction();
        Crypto cry = new Crypto();
        JavaScriptSerializer js = new JavaScriptSerializer();

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet =true)]
        public void validatelogin(string username, string password)
        {
            SqlCommand cmd = new SqlCommand();

            cmd.Parameters.AddWithValue("@LoginName", username);
            cmd.Parameters.AddWithValue("@Password", cry.Encrypt(password));
            cmd.Parameters.AddWithValue("@Prompt", "SELECT");

            SqlDataReader reader = dbf.ExecuteSP_Reader("User_SIDU", cmd);

            if (reader.Read())
            {
                Context.Response.Write("success");
                reader.Close();
            }
            else
            {
                Context.Response.Write("fail");
                reader.Close();
            }

            reader.Close();
        }
    }
}

what is wrong here?

2 Answers 2

0

You're providing a relative path in $http.get from a page that has /login/ therefore the two paths get concatenated into /login/~/loginservice.asmx/validatelogin?password=123&username=minesh while the correct path seems to be /loginservice.asmx/validatelogin

Try using a path starting with / in the request.

$http.get('/loginservice.asmx/validatelogin',...)

That should result in a correct path.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using params, please use like this:

$http({
   url: "/Home/CallMe", 
   method: "GET",
   params: {number: 4, name: "angular"}
});

For more details please look this answer Can not call Mvc Controller Action from AngularJs Controller with parameters

There are few modifications you can go with

  1. Uncomment [System.Web.Script.Services.ScriptService] because it indicates that a Web service can be invoked from script. Please refer ScriptService documentation.
  2. try adding: <system.web> <webServices> <protocols> <add name="HttpGet"/> </protocols> </webServices> </system.web>

For more details, please see this video Consuming ASP NET Web Service in AngularJS using $http

Comments

Your Answer

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