ASP.Net jQuery AJAX Error: Guid should contain 32 digits with 4 dashes

Last Reply 3 months ago By pandeyism

Posted 3 months ago

web method event is not firing getting error

"Message":"Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."

        [WebMethod]
        public static void insertbid(string username, Guid projectid)
        {
            if (!string.IsNullOrEmpty(username))
            {
                string getuser = username;
            }
        }

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    
</head>
<body>
    <form id="form1" runat="server">
  
<script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=LinkButton1]").bind("click", function () {
            ///check query string parameter
            //assign values to webmethod       
            var username = '<%=@HttpContext.Current.User.Identity.Name%>';
            //var projectid = $("[id*=HiddenField1]").val();
            alert(username);

            var projectid =  1

            alert(projectid);
            //'84abc875-97b9-4c2d-ad17-a424f135a7e1';
                $.ajax({
                    type: "POST",
                    url: "WebForm1.aspx/insertbid",
                    data: "{username: '" + username + "', projectid: '" + projectid + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",                   
                    success: function (response) {
                        alert(response.responseText);                       
                    },
                    error: function (response) { alert(response.responseText); },
                    failure: function (response) { alert(response.responseText); }
                });
                return false;            
        });
    });
</script>
        <asp:LinkButton ID="LinkButton1" runat="server">Submit</asp:LinkButton>
    </form>
</body>
</html>

 

Posted 3 months ago

Hi nauna,

Refer below sample.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
    <script type="text/javascript">
        $(function () {
            $("[id*=LinkButton1]").bind("click", function () {
                var username = '<%=@HttpContext.Current.User.Identity.Name%>';
                var projectid = createGuid();
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/insertbid",
                    data: "{username: '" + username + "', projectid: '" + projectid + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) { alert(response.responseText); }
                });
                return false;

            });
        });
        function createGuid() {
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            });
        }  
    </script>
    <asp:LinkButton ID="LinkButton1" runat="server">Submit</asp:LinkButton>
    </form>
</body>
</html>

Namespaces

 

using System.Web.Services;

Code

[WebMethod]
public static string insertbid(string username, Guid projectid)
{
    if (!string.IsNullOrEmpty(username))
    {
        string getuser = username;
    }
    return string.Format("{0},{1}", username, projectid);
}

You need to generate guid refer below link to generate guid.

https://www.c-sharpcorner.com/blogs/generate-guid-using-javascript1