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

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 对象参数,但它从未出现在那里。

我得到的只是这个错误:

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

- - 更新

这是最终结果(我必须“字符串化”对象,然后发送最终的 dataText)。 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 字符串化为 C# Webservice,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9485532/

10-09 19:51