4

I know there are a few questions out there but I've tried a lot of them and I'm still not able to even get my script to make it to the server. Here is what I currently have:

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);
        }
    }
}

Example of user object

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));
}

Why does my JSON call never make it to the server? I've put a break point at the very beginning of the function (before the return) just so I can look at the jsonUser object parameter but it never makes it there.

All I get in return is this error:

POST http://localhost:60877/Sitefinity/Services/Sandbox/SessionUsers.asmx/UpdateSessionUser 500 (Internal Server Error)

--- UPDATE

Here is the final result (I had to "stringify" the object and then the final dataText being sent). The webservice method was unchanged

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;
            }
        });
    }
8
  • 2
    500 is a server error. Your call did reach the server. Look in the Application event log to see if there's an ASP.NET warning showing you the exception that occurred. Feb 28, 2012 at 16:18
  • Good call. I'm running this locally through Visual Studio and I checked my windows application log and of course... nothing there :(
    – Miles
    Feb 28, 2012 at 16:20
  • Make sure ASP.NET Health Monitoring is turned on. <system.web> <healthMonitoring enabled="true"/></system.web> Feb 28, 2012 at 16:24
  • Can you get to the service at all. In you broweser go to: localhost:60877/Sitefinity/Services/Sandbox/SessionUsers.asmx and if so do you see the UpdateSessionUser method listed? Also, does document.location.host include the port number? Feb 28, 2012 at 16:26
  • Does it work any better if you stringify dataText? I believe I've always passed a json string rather than an object.
    – Rawling
    Feb 28, 2012 at 16:27

3 Answers 3

2

I don't how much would help:

try change this

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

to

  var dataText = JSON.stringify({ "jsonUser" : user });
3
  • 1
    I ended up having to "stringify" both items. (I added it to the bottom of my question)
    – Miles
    Feb 28, 2012 at 17:15
  • I'm not sure if you need to stringify twice, but if it works, then it's OK Feb 28, 2012 at 17:33
  • 1
    ya, I kind of got to the point with it such that it works, I'm not touching it any more ;)
    – Miles
    Feb 28, 2012 at 17:38
0

I think you need to mark your service method as JASON enabled.

[WebMethod] 
[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)] 
public bool UpdateSessionUser(object jsonUser) 
{
     return SessionUserHelper.UpdateSessionUser(new ClientSessionUser(jsonUser));
} 
0
0

I realize this question was answered a while ago, but I had similar problem, and would like to provide some details on why OP's solution works

I assume the web service expects a string, which it will Deserialize and work with

In this case, calling JSON.stringify({"jsonUser": user}) does not convert user object to string, which will cause problems on the server.

When you call JSON.stringify({"jsonUser": JSON.stringify(user)}), your user oblect is converted to string with all quotes properly escaped.

Hope this helps someone in the future.

Here's the fiddle to illustrate http://jsfiddle.net/dvwCg/2/ :

HTML

<h1>webservice breaks</h1>
<span id="s1"></span>
<h1>webservice works</h1>
<span id="s2"></span>

JS

var jsonObject = {"a":1, "b":[{"x":"test", "y":12}, {"x":"test2", "y":120}]};

$("#s1").text(JSON.stringify({"d" : jsonObject}));
$("#s2").text(JSON.stringify({"d" : JSON.stringify(jsonObject)}));

p.s.

https://stackoverflow.com/a/6323528/3661 has a lot of useful info as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.