1

I am using fluent NHibernate with WebForms and I am trying to build a page where I allow a user to post a status update on their profile page and I a using a .asmx WebService to post the data to the database and then returnig a StatusUpdate instance to the page to be used by jQuery. I have a couple problems.

1) First off when I return a string from the WebService(i was testing) the textbox where the user enters their status doesnt empty the contents. And since the page doesnt refresh even if I manally clear out the textbox and put in something else, it still posts the previous status to the database again. How do I fix this?

2) Secondly, when I return the StatusUpdate Object from the Webservice I cant the results to display anything. Like I sad, Im using jQuery to make an AJX call to the WebService.

Here is my code:

User Profile Page Javascript:

 var status1 = $("#statusBox").val();
 var userID = $("#MainContent_userID").val();
 function SetStatus() {
 $.ajax({
 type: "POST",
 url: "http://localhost/Sports/Services/UserWebService.asmx/SetStatus",
 data: '{"status": "' + status1 + '", "userID": "' + userID + '"}',
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: OnSuccess,
 error: OnError
 });
 }
 function OnSuccess(response) {
 $("#statusBox").empty();
 $("#MainContent_status").html(response.Status).fadeIn(1000); 
 }
 function OnError(request, status, error) {
 alert(request.statusText);
 }

WebService:

[WebService(Namespace = "Sports.Services")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)] 
[System.Web.Script.Services.ScriptService]
public class UserWebService : System.Web.Services.WebService
{
 private IUserSession _userSession;
 public ISession Session1
 {
 get { return NHibernateSessionModule.GetCurrentSession(); }
 } 
 [WebMethod]
 public StatusUpdate SetStatus(string status, Guid userID)
 {
 _userSession = ObjectFactory.GetInstance<IUserSession>();
 StatusUpdate update = new StatusUpdate();
 update.StatusDateTime = DateTime.Now;
 update.StatusLikes = 0;
 update.UserID = userID;
 update.Status = status;
 Session1.SaveOrUpdate(update);
 return update;
 }
}

Any help would be greatly appreciated... Ive looked at probably 100 pages trying to find an answer and none have worked..

asked Aug 21, 2010 at 1:46

1 Answer 1

5

For #1, you need to move the .val() calls inside the method so they're fetched at the correct time, like this:

function SetStatus() {
 var status1 = $("#statusBox").val();
 var userID = $("#MainContent_userID").val();
 $.ajax({
 type: "POST",
 url: "http://localhost/Sports/Services/UserWebService.asmx/SetStatus",
 data: '{"status": "' + status1 + '", "userID": "' + userID + '"}',
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: OnSuccess,
 error: OnError
 });
}

Though you should consider making this as an object and using .stringify to handle any special characters.


For #2, asmx web services like to wrap the object, so it doesn't look like this:

{ "Status": "My Status", "UserID": 12, "StatusLikes": 0..... }

It actually looks like this:

{ "d": { "Status": "My Status", "UserID": 12, "StatusLikes": 0..... } }

So instead of this:

$("#MainContent_status").html(response.Status).fadeIn(1000);

You need this:

$("#MainContent_status").html(response.d.Status).fadeIn(1000);
answered Aug 21, 2010 at 2:00
Sign up to request clarification or add additional context in comments.

2 Comments

You definitely fixed one thing I forgot to mention by putting the .val) calls inside the function.. That was a total oversight by me. Thanks so much for that.. Now for one prob I still have, When I post the update to the database, and return the object, he new status doesnt show in the status area. It still shows the old one. What could be causing that? Thats the only problem I have left now. Thanks again for your help already..
Nm.. I figured it out finally... LOL Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.