locked
Pass parameter with jquery and Webservice RRS feed

  • Question

  • User932259438 posted

    hello,

    i need to pass parameter userid to my webservice and not works with my code.

    I need to display my data in json and then display depends of my userid param.

    My code webservice:
     public string HelloWorld(Guid userid)
        {
     var query = from t1 in db.aspnet_Users
                        where t1.UserId == userid
                        select new Uporabniki() { UserName = t1.UserName };


            return new JavaScriptSerializer().Serialize(query.ToList());
        }

    $.ajax({
    type: "GET",
    url: "https://www.domainname.com/webservice/login.asmx/HelloWorld",
    // The key needs to match your method's input parameter (case-sensitive).
    // data: JSON.stringify({ name: "test" }),
    contentType: "application/json; charset=utf-8",
     data: "{'UserId':'eb457f00-dd2c-441e-ad0a-2fc7fa814ccb'}",
    dataType: "json",
    success: function (data) { alert(data.d); },
    failure: function (errMsg) {
    alert(errMsg);
    }
    });
    Plesae help.

    Saturday, December 28, 2019 11:00 AM

All replies

  • User1535942433 posted

    Hi progy85,

    Accroding to your codes, I creat a demo.However,I meet the mistake of 500. It alert that Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.
    I find the UserId in the ajax and the userid in the .asmx file is different.
    I suggest you could change the userid to be same(including about character case) and replace Guid to string in the webmethod.

    More details ,you could refer to below codes:

    <script type="text/javascript">
    
            $(document).ready(function () {
    
                    $.ajax({
    
                        type: "POST",
    
                        url: "login.asmx/HelloWorld",
    
                        data: "{'UserId':'eb457f00-dd2c-441e-ad0a-2fc7fa814ccb'}",
    
                        dataType: "json",
    
                        contentType: "application/json; charset=utf-8",
    
                        success: function (data) { alert(data.d); },
    
                        failure: function (errMsg) {
    
                            alert(errMsg);
    
                        }
    
                    });
    
            });
    
        </script>

    Webservice:

    [WebMethod]
    
            public string HelloWorld(string UserId)
    
            {
    
                string str, strSql;
    
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542ConnectionString"].ConnectionString;
    
                SqlConnection conn = new SqlConnection(str);
    
                strSql = "select UserName from User where UserId='" + UserId + "'";
    
                conn.Open();
    
                SqlCommand cmd = new SqlCommand(strSql, conn);
    
                var dr = cmd.ExecuteReader();
    
                var query = new List<string>();
    
                while (dr.Read())
    
                {
    
                    query.Add(dr["UserName"].ToString());
    
                }
    
                conn.Close();
    
                return new JavaScriptSerializer().Serialize(query.ToList());
    
                //return UserId;
    
            }

    Result:

    Best regards,

    Yijing Sun

    Monday, December 30, 2019 9:52 AM