无法在Web服务中获取数据 [英] Unable to get data in a web service
问题描述
你好,我正在调用一个web服务,我在其中创建了一个具有两个属性的对象类型的变量,但是调用正确地进入服务但是我无法使用我的类在我的web方法中接收发送的对象。
以下是我的JavaScript代码
hello, I'm calling a web service in which i have created a variable of object type which have two attribute however call goes to the service properly but I'm unable to receive sent object inside my web method using my class.
following is my JavaScript code
function newfun() {
debugger
var DbClass = new Object();
DbClass.col1 = document.getElementById("txtName").value; //'hello';
DbClass.col2 = document.getElementById("txtEmail").value; //'vaibhav';
alert(JSON.stringify(DbClass));
$.ajax({
type: "POST",
url: "WebService.asmx/DBAddClass",
data: "{obj:" + JSON.stringify(DbClass) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(value) {
alert("sucess");
},
error: function(value) {
//error_callback(response.responseText);
alert("Error Occured");
}
});
}
以下是我的网络方法
following is my webmethod
<WebMethod()> _
Public Sub DBAddClass(ByVal obj() As DbClass)
Try
Dim clsObj As New DbClass
'' clsObj._col1 = obj.col1
'' clsObj._col2 = obj.col2
Dim constr As String = ConfigurationManager.ConnectionStrings("myCon").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("INSERT INTO tab1 (Col1, Col2) VALUES (@Name, @Country)")
cmd.Parameters.AddWithValue("@Name", obj(0)._col1) 'obj("col1"))
cmd.Parameters.AddWithValue("@Country", obj(0)._col2) ' obj("col2"))
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Catch ee As Exception
End Try
End Sub
我也创建了类,如下所示
also I have created class as well which is as follows
Public Class DbClass
Private col1 As String
Public Property _col1() As String
Get
Return col1
End Get
Set(ByVal value As String)
col1 = value
End Set
End Property
Private col2 As String
Public Property _col2() As String
Get
Return col2
End Get
Set(ByVal value As String)
col2 = value
End Set
End Property
End Class
我想在
I want to catch data in the object of
DbClass
的对象中捕获数据,这些数据将从ajax调用发送。任何帮助都对我有用。
which will be send from a ajax call. any help will be of great use for me.
推荐答案
.ajax({
type:POST,
url:WebService。 asmx / DBAddClass,
data:{obj:+ JSON.stringify(DbClass)+},
contentType:application / json; charset = utf-8,
dataType:json,
成功:函数(值){
alert(sucess);
},
错误:函数(值){
/ /error_callback(response.responseText);
alert(Error Occured);
}
});
}
.ajax({ type: "POST", url: "WebService.asmx/DBAddClass", data: "{obj:" + JSON.stringify(DbClass) + "}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(value) { alert("sucess"); }, error: function(value) { //error_callback(response.responseText); alert("Error Occured"); } }); }
以下是我的网络方法
following is my webmethod
<WebMethod()> _
Public Sub DBAddClass(ByVal obj() As DbClass)
Try
Dim clsObj As New DbClass
'' clsObj._col1 = obj.col1
'' clsObj._col2 = obj.col2
Dim constr As String = ConfigurationManager.ConnectionStrings("myCon").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("INSERT INTO tab1 (Col1, Col2) VALUES (@Name, @Country)")
cmd.Parameters.AddWithValue("@Name", obj(0)._col1) 'obj("col1"))
cmd.Parameters.AddWithValue("@Country", obj(0)._col2) ' obj("col2"))
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Catch ee As Exception
End Try
End Sub
我也创建了类,如下所示
also I have created class as well which is as follows
Public Class DbClass
Private col1 As String
Public Property _col1() As String
Get
Return col1
End Get
Set(ByVal value As String)
col1 = value
End Set
End Property
Private col2 As String
Public Property _col2() As String
Get
Return col2
End Get
Set(ByVal value As String)
col2 = value
End Set
End Property
End Class
我想在
I want to catch data in the object of
DbClass
的对象中捕获数据,这些数据将从ajax调用发送。任何帮助都对我有用。
which will be send from a ajax call. any help will be of great use for me.
问题是你的方法需要一个DBClass
对象的数组,但是调用代码只传递一个DBClass
对象。
因为你只在方法中使用一个对象,最简单的解决方案是将方法更改为接受单个对象:
The problem is that your method is expecting an array ofDBClass
objects, but the calling code is only passing a singleDBClass
object.
Since you only ever use a single object in the method, the simplest solution is to change the method to accept a single object:
<WebMethod()> _
Public Sub DBAddClass(ByVal obj As DbClass)
Dim constr As String = ConfigurationManager.ConnectionStrings("myCon").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("INSERT INTO tab1 (Col1, Col2) VALUES (@Name, @Country)", con)
cmd.Parameters.AddWithValue("@Name", obj._col1)
cmd.Parameters.AddWithValue("@Country", obj._col2)
con.Open()
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
注意:空 Catch
块是一个非常糟糕的主意。只捕获代码可以处理的特定异常,并记录任何不传播给调用者的异常。
如果要传递一个对象数组该方法,然后你需要传递正确的JSON:
NB: An empty Catch
block is a very bad idea. Only catch the specific exceptions which your code can handle, and log any exceptions which don't propagate to the caller.
If you want to pass an array of objects to the method, then you need to pass the correct JSON:
data: JSON.stringify({ obj: [ DbClass ] }),
(注意 obj
值附近的额外方括号,表示一个数组。 )
(Note the extra square brackets around the obj
value, indicating an array.)
感谢所有人试图帮助我。但是你的解决方案都没有正常工作。希望你们能很快帮助我。
Thanks to all for trying to help me. but none of your solution has worked correctly. Hope you guys help me out very soon.
这篇关于无法在Web服务中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!