gpt4 book ai didi

javascript - 使用 JSON.stringify 将 jQuery AJAX 发布到我的 ASP.NET web 服务

转载 作者:行者123 更新时间:2023-11-30 15:05:36 24 4
gpt4 key购买 nike

我正在尝试触发 jQuery AJAX 发布到我的 ASP.NET 网络服务。但是,即使执行了 subscribeToSearch 方法,当我访问该方法中的 strSubscriber.email 变量时,它还是空的。为什么?

这些是我在发布之前记录的可变值:

email: YES@NO.COM
objecttype: 15
provinceid: 7
city: "test"
distance: 0
minprice: 0
maxprice: 0
frequency: 0
rooms: 0
surface: 0

这是 AJAX 调用:

   $.ajax({
type: "POST",
url: '/api/subscribetosearch',
async: true,
data: JSON.stringify({
str: {
'objecttype': objecttype,
'email': email,
'provinceid': provinceid,
'city': city,
'distance': distance,
'minprice': minprice,
'maxprice': maxprice,
'frequency': frequency,
'rooms': rooms,
'surface': surface
}
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function () {
},
success: function (msg) {


}
});

网络方法签名:

Ihouse.vb

    <OperationContract()>
<Web.WebInvoke(Method:="POST", ResponseFormat:=Web.WebMessageFormat.Json, BodyStyle:=Web.WebMessageBodyStyle.WrappedRequest,
UriTemplate:="subscribetosearch")>
Function subscribeToSearch(ByVal str As GlobalFunctions.SearchSubscriber) As Stream

房子.svc.vb

Public Function subscribeToSearch(ByVal strSubscriber As GlobalFunctions.SearchSubscriber) As Stream Implements Ihouse.subscribeToSearch
LogError("subscribeToSearch hit")
LogError("subscribeToSearch email: " + strSubscriber.email)
End Function

我在 Google Chrome 开发控制台中查看了 header ,这是请求负载:

enter image description here

这是 SearchSubscriber 类。

Public Class GlobalFunctions

<Runtime.Serialization.DataContract>
Public Class SearchSubscriber
<Runtime.Serialization.DataMember>
Private _objecttype As Integer
Public Property objecttype() As Integer
Get
Return _objecttype
End Get
Set(ByVal value As Integer)
_objecttype = value
End Set
End Property

Private _email As String
Public Property email() As String
Get
Return _email
End Get
Set(ByVal value As String)
_email = value
End Set
End Property

Private _provinceid As Integer
Public Property provinceid() As Integer
Get
Return _provinceid
End Get
Set(ByVal value As Integer)
_provinceid = value
End Set
End Property

Private _city As String
Public Property city() As String
Get
Return _city
End Get
Set(ByVal value As String)
_city = value
End Set
End Property

Private _distance As Integer
Public Property distance() As Integer
Get
Return _distance
End Get
Set(ByVal value As Integer)
_distance = value
End Set
End Property

Private _minprice As Integer
Public Property minprice() As Integer
Get
Return _minprice
End Get
Set(ByVal value As Integer)
_minprice = value
End Set
End Property

Private _maxprice As Integer
Public Property maxprice() As Integer
Get
Return _maxprice
End Get
Set(ByVal value As Integer)
_maxprice = value
End Set
End Property

Private _frequency As Integer
Public Property frequency() As Integer
Get
Return _frequency
End Get
Set(ByVal value As Integer)
_frequency = value
End Set
End Property

Private _rooms As Integer
Public Property rooms() As Integer
Get
Return _rooms
End Get
Set(ByVal value As Integer)
_rooms = value
End Set
End Property

Private _surface As Integer
Public Property surface() As Integer
Get
Return _surface
End Get
Set(ByVal value As Integer)
_surface = value
End Set
End Property

End Class
End Class

更新 1

按照@Nkosi 的建议,现在我有了这个:

//construct the object to be posted.
var searchSubscriber = {
objecttype: objecttype,
email: email,
provinceid: provinceid,
city: city,
distance: distance,
minprice: minprice,
maxprice: maxprice,
frequency: frequency,
rooms: rooms,
surface: surface
};

$.ajax({
type: "POST",
url: '/api/subscribetosearch',
async: true,
data: JSON.stringify(searchSubscriber),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function () {
},
success: function (msg) {

}
});

请求负载现在是:

enter image description here

服务器上的以下方法被命中,但是strSubscriber对象什么都没有...

Public Function subscribeToSearch(ByVal strSubscriber As GlobalFunctions.SearchSubscriber) As Stream Implements Ihouse.subscribeToSearch
LogError("subscribeToSearch HIT") <---- this line gets executed

If strSubscriber Is Nothing Then
LogError("subscribeToSearch strSubscriber IS NOTHING") <---- this line gets executed
Else
LogError("subscribeToSearch strSubscriber IS SOMETHING")
End If

End Function

最佳答案

更新

However, even though the subscribeToSearch method is executed, when I access the strSubscriber.email variable in that method it's empty. Why?

注意到你不见了<Runtime.Serialization.DataMember>您要在服务器上访问的所有属性的属性。使用 DataContract 时您需要正确标记在序列化/反序列化模型时要使用的所有属性。因此,由于未标记成员,因此在反序列化数据时未填充该成员。

Public Class GlobalFunctions
<Runtime.Serialization.DataContract>
Public Class SearchSubscriber
<Runtime.Serialization.DataMember>
Public Property objecttype As Integer

<Runtime.Serialization.DataMember>
Public Property email As String

<Runtime.Serialization.DataMember>
Public Property provinceid As Integer

<Runtime.Serialization.DataMember>
Public Property city As String

<Runtime.Serialization.DataMember>
Public Property distance As Integer

<Runtime.Serialization.DataMember>
Public Property minprice As Integer

<Runtime.Serialization.DataMember>
Public Property maxprice As Integer

<Runtime.Serialization.DataMember>
Public Property frequency As Integer

<Runtime.Serialization.DataMember>
Public Property rooms As Integer

<Runtime.Serialization.DataMember>
Public Property surface As Integer

End Class
End Class

原创

没有根据服务的预期正确构建有效负载。

该函数需要一个 GlobalFunctions.SearchSubscriber ,而不是具有 str 的对象GlobalFunctions.SearchSubscriber 属性这是原始问题中的代码发送的内容。

对其进行简化,以便更轻松地进行故障排除。

//construct the object to be posted.
var searchSubscriber = {
objecttype: objecttype,
email: email,
provinceid: provinceid,
city: city,
distance: distance,
minprice: minprice,
maxprice: maxprice,
frequency: frequency,
rooms: rooms,
surface: surface
};

$.ajax({
type: "POST",
url: '/api/subscribetosearch',
async: true,
data: JSON.stringify(searchSubscriber),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function () {
},
success: function (msg) {

}
});

Web 方法签名也需要更新:

Ihouse.vb

<OperationContract()>
<Web.WebInvoke(Method:="POST", ResponseFormat:=Web.WebMessageFormat.Json, BodyStyle:=Web.WebMessageBodyStyle.Bare, UriTemplate:="subscribetosearch")>
Function subscribeToSearch(ByVal model As GlobalFunctions.SearchSubscriber) As Stream

关于javascript - 使用 JSON.stringify 将 jQuery AJAX 发布到我的 ASP.NET web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45776373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com