[RESOLVED]issue with webmethod not able to get value in hidden field & asmx webmethod doesnot get executed

<script type="text/javascript">
    $(document).ready(function () {
        $("#<%=txtName.ClientID %>").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '<%=ResolveUrl("~/Details.aspx/GetCustomers") %>',
                    data: "{ 'prefix': '" + request.term + "'}",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('-')[0],
                                val: item.split('-')[1]
                            }
 
                        }));
 
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }
                });
            },
            select: function (e, i) {
                $("#<%=hdnId.ClientID %>").val(i.item.val);
alert(i.item.val);
            },
            minLength: 3
        });
    });
</script>

using the above script
issues
1) the webmethod is in my code behiand page not in seperate webservice ie asmx page  as using a seperate asmx page the web method doesnot get executed at all
the web application uses master page

2) with above script getting error

Message: ‘length’ is null or not an object
URI: http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js]

how do i get the alert for hidden field value

i tried calling the helloworld method from asmx page even thats not getting executed

Hi svibuk,

Did you write your code behind method as :

[WebMethod]
public static string GetCustomers()
{
//do Something
}

Hope this helps.

i have

 <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Shared Function GetCustomers(ByVal prefix As String) As String()

Hi svibuk,

Thanks for your post.

svibuk

the webmethod is in my code behiand page not in seperate webservice ie asmx page  as using a seperate asmx page the web method doesnot get executed at all

Firstly, if you’d like to call the web service from script, you should uncomment the following line.

<System.Web.Script.Services.ScriptService()> _

Secondly, you should make sure the url is correct.

svibuk

Message: ‘length’ is null or not an object
URI: http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js]

Thirdly,  you could try other js file.

Besides, here is a sample to call web service from script by using ajax, you could refer to it.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../../Scripts/jquery-1.8.2.js"></script>
    <script>
        $(function(){
            $("#btncall").click(function () {

                $.ajax({   
                    type: "Post",    
                    url: "WebService.asmx/HelloWorld",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var str = data.d;
                        alert(str);
                    },
                    error: function (err) {
                        alert(err);
                    }
                });

            })
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="btncall" type="button" value="CallWebService " />
    </div>
    </form>
</body>
</html> 

Web Service:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data.SqlClient

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class WebService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function

End Class 

Hope it will be helpful to you.

Best Regards,

Fei Han

Leave a Reply