[RESOLVED]$.ajax with web service

Hi,

I can seem to insert my data with the code below.

each time i try, i get the error message : InsertProject webserive method name is not valid.

please where im i getting it wrong?

Jquery Code
$.ajax({

type: "POST",
datatype: "json",
contenttype: "application/json; charset=utf-8",
url: "../Services/MyWebService.asmx/InsertProject",
data:data,
data:JSON.stringify(data),
success: function (data) {
var rg = data.d;
alert(rg);
},
error: function (xMLHttpRequest) {
alert(xMLHttpRequest.responseText);
}
});

Web Service Code
<System.Web.Script.Services.ScriptService()> _
<WebMethod()> _

Public Function InsertProject(details As ProjectDetails) As String

Dim strCon As String = ConfigurationManager.ConnectionStrings("PTT").ConnectionString
Dim con As New SqlConnection(strCon)
Dim com As New SqlCommand()
com.CommandText = "usp_InsertProjects"
com.CommandType = CommandType.StoredProcedure
com.Connection = con

com.Parameters.AddWithValue("@ProjectID", CType(details.projectID, Guid))
com.Parameters.AddWithValue("@ProjectName", details.projectName)
com.Parameters.AddWithValue("@ProjectDescription", details.projectDescription)
com.Parameters.AddWithValue("@SequenceNumber", details.sequenceNumber)
com.Parameters.AddWithValue("@LastUpdateTime", details.lastUpdateTime)
Try
con.Open()
com.ExecuteNonQuery()
Catch ex As Exception
Finally
con.Close()
End Try

Return "Record Inserted"
End Function

Make a method as static  i mean shared function and try it

Public Shared Function InsertProject(details As ProjectDetails) As String

Hi websyd,

Thanks for your post.

Firstly, you could search about “webserive method name is not valid”, you will get some similar issues.

websyd

$.ajax({

type: "POST",
datatype: "json",
contenttype: "application/json; charset=utf-8",
url: "../Services/MyWebService.asmx/InsertProject",
data:data,
data:JSON.stringify(data),

success: function (data) {
var rg = data.d;
alert(rg);
},
error: function (xMLHttpRequest) {
alert(xMLHttpRequest.responseText);
}
});

Secondly, according to your code, there are two data in setting.

Besides, here is a working 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

for some reasons this code is not working. 

could be version issue? i use 4.5

Hi websyd,

websyd

for some reasons this code is not working. 

could be version issue? i use 4.5

Do you mean that my sample does not work on your side? if that is the case, you could test the sample in .net framework 4.0. Besides, you could try to use other js file.

Best Regards,

Fei Han

Leave a Reply