JSON binds to C # Webservice

I know there are a few questions, but I tried a lot of them, and I still can't even get my script to make it on the server. Here is what I have now:

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

Custom Object Example

 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" 

FROM#

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

Why does my JSON call never hit the server? I set a breakpoint at the very beginning of the function (before returning) so that I can look at the parameter of the jsonUser object, but it never does it there.

All I get in return is an error:

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

--- UPDATE

Here is the final result (I had to “pull” the object and then send the final data file). Webservice method has not changed

 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; } }); } 
+4
source share
3 answers

I can not help:

try changing this

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

to

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

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

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

I understand that this question was asked some time ago, but I had a similar problem and I would like to provide some details on why the OP solution works

I assume the web service is expecting a string that will deserialize and work with

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

When you call JSON.stringify({"jsonUser": JSON.stringify(user)}) , your user divlect is converted to a string with all quotation marks that have been escaped properly.

Hope this helps someone in the future.

Here is a script 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)})); 

ps

fooobar.com/questions/35524 / ... contains a lot of useful information.

0
source

Source: https://habr.com/ru/post/1398844/


All Articles