gpt4 book ai didi

c# - JSON stringify 到 C# Webservice

转载 作者:行者123 更新时间:2023-11-30 12:33:19 25 4
gpt4 key购买 nike

我知道那里有一些问题,但我已经尝试了很多,但我仍然无法让我的脚本发送到服务器。这是我目前拥有的:

Javascript

function UpdateSessionUser(user)
{
if (user != null)
{
var targetPage = "http://" + document.location.host + "/Sitefinity/Services/Sandbox/SessionUsers.asmx/UpdateSessionUser";
var dataText = { "jsonUser" : JSON.stringify(user) };

try
{

$.ajax({
type: "POST",
url: targetPage,
data: dataText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response)
{
alert(response.d);
return true;
},
failure: function (msg)
{
alert(msg);
return false;
}
});
}
catch (err)
{
alert(err);
}
}
}

用户对象示例

Object
BaseID: "fe85149c-71f2-4c61-b7c6-a00300e2f84e"
HasChanged: true
IsReferralReceived: false
IsReferralRequired: true
IsSeatApproved: true
Name: "Miles"
ReferralFromUser: null
ReferralFromUserID: null
ReferralReceivedBy: null
ReferralReceivedByUserID: null
ReferralReceivedOn: "/Date(-62135578800000)/"
RegisteredOn: "1330281960000"
SeatApprovedBy: null
SeatApprovedByUserID: null
SeatApprovedOn: "/Date(-62135578800000)/"
SeatNumber: "2"
SessionID: "d0773d5e-aeeb-4b9c-b606-0a564d6c5845"
UserID: "6af2fd9e-b4b6-4f5a-8e9c-fe7ec154d4e5"
__type: "SandboxClassRegistration.SessionUserField.ClientSessionUser"

C#

[WebMethod]
public bool UpdateSessionUser(object jsonUser)
{
return SessionUserHelper.UpdateSessionUser(new ClientSessionUser(jsonUser));
}

为什么我的 JSON 调用从未到达服务器?我在函数的最开头(返回之前)放置了一个断点,这样我就可以查看 jsonUser 对象参数,但它永远不会到达那里。

我得到的返回是这个错误:

发布 http://localhost:60877/Sitefinity/Services/Sandbox/SessionUsers.asmx/UpdateSessionUser 500(内部服务器错误)

---更新

这是最终结果(我必须“字符串化”对象,然后发送最终的数据文本)。 webservice方法没变

function CallWebServiceToUpdateSessionUser(target, user)
{
var dataText = { "jsonUser": JSON.stringify(user) };

$.ajax({
type: "POST",
url: target,
data: JSON.stringify(dataText),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response)
{
alert(response.d);
return true;
},
failure: function (msg)
{
alert(msg);
return false;
}
});
}

最佳答案

我不知道有多大帮助:

尝试改变这个

 var dataText = { "jsonUser" : JSON.stringify(user) };

  var dataText = JSON.stringify({ "jsonUser" : user });

关于c# - JSON stringify 到 C# Webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9485532/

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